使用XCTest在单元测试中获取变量的nil值 [英] Getting nil value for the variable in Unit Test using XCTest

查看:123
本文介绍了使用XCTest在单元测试中获取变量的nil值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

appDelegate实例显示nil值,测试用例testAppDelegate失败。
同样适用于apple开发者网站提供的示例代码,但是正在使用SenTestCase,请帮助我,甚至根据WWDC 2013视频设置目标在Xcode 5 session 409中测试

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");
}


推荐答案

Anton给出了正确的建议。
但如果它不起作用(在我的情况下)尝试:

Anton gives right suggestion. But if it doesn't works (as in my case) try to:


  1. 删除现有的测试目标

  1. Remove your existing Test Target

创建新的测试目标(左侧窗格中的第5个选项卡 - >点击底部的 + 左上角 - >新测试目标)和

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

在出现的窗口中,不要忘记选择您的应用程序作为测试目标的目标。

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

将所有TestCases文件添加到新目标。

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.

这篇关于使用XCTest在单元测试中获取变量的nil值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆