Swift, access modifiers and unit testing
I just upgraded to Xcode 6 beta 4, where the Swift compiler now supports access modifiers.
That caused a problem for me, since my unit tests now fail to compile (due to the classes not being public).
The simple solution is of course to make all tested classes public, but that feels like a hack (my personal preference is to write unit tests even on non-public classes).
In .NET and Java, you can normally allow unit tests assembly-level (or bundle-level in Java/OSGi) access to the assembly under test from the unit test assembly. I did not understand how to do something similar in Swift. Do I really have to make all my classes public to unit test them?
- Increase NavigationBar height
- Automatically scrolling to the bottom of a table with UITableViewAutomaticDimension row height? - Swift, iOS 8+
- NSFetchedResultsController swift sections
- How to sort an Array of Tuples?
- Make swift class conform to protocol - at static/class level
- Firebase in Swift nested query not working properly
4 Solutions Collect From Internet About “Swift, access modifiers and unit testing”
This is a known issue and mentioned in the Beta 4 release notes. You might want to hold off changing your designs until more information is provided.
We’re aware that our access control design isn’t great for unit testing (and this was in the release notes), we’re evaluating the situation to see what we can do.
— Chris Lattner
A limitation of the access control system is that unit tests cannot interact with the classes and methods in an application unless they are marked public. This is because the unit test target is not part of the application module.
— Xcode beta 4 release notes
With Swift 2 you are now allowed to test your class without have to marked it as public. You just have to use the keyword
@testable and the compiler will take care of rest.
Slide from What’s new in Xcode WWDC 2015:
You can just add the source files from your target to the test target. Then they will be a part of your test target and you will be able to access them.
I think I have a better solution than making everything public. Simply make the StoryBoard a member of the test target just as you do with all your ViewControllers. Then create the StoryBoard in your test class using your test bundle instead of using nil or the main bundle. Check my post here for sample code.
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType)) vc = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as LoginViewController vc.loadView()
- Set Objective-C property by string
- Sorting NSString values as if NSInteger using NSSortDescriptor
- What is “Constrain to margin” in Storyboard in Xcode 6
- Phonegap – Save image from url into device photo gallery
- The operation couldn’t be completed. (Cocoa error -1.) – PHPhotoLibrary
- ld: framework not found GRPCClient
- KVO working once in Swift
- Swift can't show back button after hiding it
- iOS 8 SDK, Swift, MapKit Drawing a Route
- Unable to open liblaunch_sim.dylib
- Suitable api to get product details from scanned barcode iphone app?
- how to draw automatic line between two tap points in a view in iphone
- UnsafeMutablePointer<Void> to Concrete Object Type
- Error mapping single JSON object in Restkit
- How to use AVCaptureSession with Slide Over and Split View in iOS 9?