问题
So I am trying to write down some test cases for one of my controller class,
- My viewcontroller is defined in objective c.
- I am writing test cases in swift.
- I have included my viewcontroller class in both my main target and my test target.
- I have also added the storybaord in both test and main target.
In my test I am trying to load the view controller from story board, below is the code (this code is working fine in my normal project from swift class)
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.init(for: self.classForCoder))
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController")
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
controller = viewController as! LoginViewController
print output
LoginView Controller
__ABT_LoginViewController
Last line of my code is getting failedi.e
controller = viewController as! LoginViewController
error I am getting below
Could not cast value of type '__ABT_LoginViewController' (0x600000053b30) to 'LoginViewController' (0x11e88de30).
Need your help guys
Updated So above issue resolved after following the answer by @dashandrest, Now I am facing new issue, I have created a TestAppdelegate file for test cases, in my TestAppDelegate class I have this statement import "App-swift.h now after compiling the whole code it is giving an error inside App-swift.h file mentioning cannot import module AppName, My main target module name is AppName whereas test target module name is AppNameTest,
#import "App-swift.h"
Now I am getting compiling error inside App-swift.h file.
#if defined(__has_feature) && __has_feature(modules)
@import ObjectiveC;
@import AppName; ///this line generate compile errror, module AppName not found
@import XCTest;
#endif
Updated
So there were multiple issues in my app which I resolved, definitely @DashAndRest input help me in identifying those, below are the things I correct in order to make things work.
- Remove all code files from test target except the test cases.
- Make sure that I have separate Module name for my main target and test target.
- Make sure that My Pods file does have installing the libraries for my test target also.
- Write test cases in Swift for the safe side so that I don't need to include Appname-swift.h in test cases.
- Use @testable import AppModuleName (as suggested by @DashAndRest)
回答1:
This is not required:
3 I have included my viewcontroller class in both my main target and my test target.
4 I have also added the storybaord in both test and main target.
Try unchecking Test target for both view controller and storyboard.
And then in your test file just include main target as:
@testable import Your-Main-Target-Name
Update
Still App-swift.h is confusing.
But if you want to test LoginViewController, here is the solution:
Add Bridging Header to your main AppName target and import all the objective-c classes to it, like:
#ifndef AppName_Bridging_Header_h
#define AppName_Bridging_Header_h
#import "LoginViewController.h"
#endif
After this, your LoginViewController class will be available for all other swift classes to use, including test target.
Add new file like LoginVCTests.swift in test target and add following code to it-
import XCTest
@testable import AppName
class LoginVCTests: XCTestCase {
func testSomeLogic() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as! LoginViewController
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
//test on viewController
}
}
Hope this will solve your problem!
来源:https://stackoverflow.com/questions/44138661/unittest-in-swift-ios-storyboard-failed-to-cast-viewcontroller-class-defined