I created a subclass of UICollectionViewController
and called it LibraryCollectionViewController
, and marked both checkboxes for the project and test. Also it's storyboard id is LibraryCollectionViewController
and set its corresponding class on interface builder.
Being new to Swift and iOS Testing I went and attempted to instantiate said view controller inside my test case as follows:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let libraryCollectionVC = storyboard.instantiateViewControllerWithIdentifier("LibraryCollectionViewController") as! LibraryCollectionViewController
And I'm getting the following error:
Could not cast value of type 'MyApp.LibraryCollectionViewController' (0x1010b01f0) to 'MyAppTests.LibraryCollectionViewController' (0x10bd7f580).
So it is clear that it isn't casting, but as I'm still new to Swift, I find myself unable to determine if the error itself is being caused by:
as!
is incorrectAny help would be appreciate, thanks in advance.
Update 1: When I was creating the view controller, the last step shows 2 checkboxes under a Target header, I enabled both of them, could that be the cause?
My problem was I was referencing NSBundle.mainBundle()
from the unit tests whereas I needed to use:
let bundle = Bundle(for: self.classForCoder)
This can happen if a class is a member of two targets (maybe two frameworks?) - and your app is somehow referencing one framework while the unit test target is referencing the second target.
So one of the compiled artefacts has a fully-qualified type Framework1.MyClass
while the other is Framework2.MyClass
- and they're different classes at runtime because they have different namespaces. (Even though both classes are compiled from the same Swift source file)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With