问题
The appDelegate instance is showing nil value, test case "testAppDelegate" is getting failed. The same is working in the sample code provided by apple developer site but there SenTestCase is being used, please help me out, even the target is set as per the WWDC 2013 video "Testing in Xcode 5 session 409"
@interface Tests : XCTestCase
{
AppDelegate *appDelegate;
AppViewController *appVC;
UIView *appView;
}
@end
@implementation Tests
- (void)setUp
{
[super setUp];
appDelegate = [[UIApplication sharedApplication] delegate];
appVC = appDelegate.appViewController;
appView = appVC.view;
}
- (void)tearDown
{
[super tearDown];
}
- (void)testAppDelegate
{
XCTAssert(appDelegate, @"Cannot find the application delegate");
}
- (void)testCheckForViewInitializatio
{
XCTAssert(appVC, @"AppViewController initialized");
}
回答1:
Try this:
- go to Project Settings
- select your test target
- in General tab switch Target section to your main target
Run again
回答2:
Anton gives right suggestion. But if it doesn't works (as in my case) try to:
Remove your existing Test Target
Create new Test Target (5-th tab in left pane -> click on
+
in bottom left corner -> New Test Target) andIn appeared window don't forget to choose your application as target of your test target.
Add all your TestCases files to new target.
I don't know the reason, but after this action it start working correctly, when I run tests with new target.
来源:https://stackoverflow.com/questions/21530605/getting-nil-value-for-the-variable-in-unit-test-using-xctest