Getting nil value for the variable in Unit Test using XCTest

冷暖自知 提交于 2019-12-24 06:44:45

问题


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:

  1. Remove your existing Test Target

  2. Create new Test Target (5-th tab in left pane -> click on + in bottom left corner -> New Test Target) and

  3. In appeared window don't forget to choose your application as target of your test target.

  4. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!