我对这个单元测试做错了什么?

时间:2014-04-01 19:23:35

标签: ios unit-testing

我正在尝试在我的iOS应用上测试我的重置按钮。基本上,在重置方法中,它会重置所有文本字段。所以我通过将record.salesamount.text分配给一串数字然后执行resetEverything来测试它。但是,在NSLog中,我对前后日志都获得了(null)。我究竟做错了什么?

- (void)testResetEverything
{
RecordViewController *record = [[RecordViewController alloc]init];
record.SalesAmounttext.text = @"1234567890123456";
NSLog(@"before %@",record.SalesAmounttext.text);
[record resetEverything];
NSLog(@"after %@",record.SalesAmounttext.text);
XCTAssertEqualObjects(record.SalesAmounttext.text, nil, "Should be a nil");
}

1 个答案:

答案 0 :(得分:2)

使用前初始化record.SalesAmounttext

- (void)testResetEverything
{
RecordViewController *record = [[RecordViewController alloc]init];
record.SalesAmounttext = [[UITextField alloc] init]; //Write this line before use record.SalesAmounttext
record.SalesAmounttext.text = @"1234567890123456";
NSLog(@"before %@",record.SalesAmounttext.text);
[record resetEverything];
NSLog(@"after %@",record.SalesAmounttext.text);
XCTAssertEqualObjects(record.SalesAmounttext.text, nil, "Should be a nil");
}

如果它是outlet,则record.SalesAmounttext会自动初始化。但在文本情况下,您需要自己初始化它,因为您没有从storyboard / xib加载它。