我正在使用XCtest来测试视图的标题.试着养成先写测试的习惯.设置看起来像
- (void)setUp
{
[super setUp];
self.appDelegate = [[UIApplication sharedApplication] delegate];
self.tipViewController = self.appDelegate.tipViewController;
self.tipView = self.tipViewController.view;
self.settingsViewController = self.appDelegate.settingsViewController;
self.settingsView = self.settingsViewController.view;
}
Run Code Online (Sandbox Code Playgroud)
问题是"settingsViewController".我有两个实际测试功能:
- (void) testTitleOfMainView{
XCTAssertTrue([self.tipViewController.title isEqualToString:@"Tip Calculator"], @"The title should be Tip Calculator");
//why does this not work?
// XCTAssertEqual(self.tipViewController.title, @"Tip Calculator", @"The title should be Tip Calculator");
}
- (void) testTitleOfSettingsView{
//make the setttings view visible
[self.tipViewController onSettingsButton];
//test the title
XCTAssertTrue([self.settingsViewController.title isEqualToString:@"Settings"], @"The title should be Settings");
}
Run Code Online (Sandbox Code Playgroud)
"testTitleOfMainView"有效.但是"testTitleOfSettingsView失败,因为self.settingsViewController是nil.我可以理解为什么.视图还没有初始化.所以我尝试将消息发送到主控制器,使settignscontroller在视图中
[self.tipViewController onSettingsButton];
Run Code Online (Sandbox Code Playgroud)
settingsController仍为零.我应该使用嘲笑吗?有人建议我用另一个问题 xctest - 如何测试按钮按下时是否加载新视图
我应该将设置视图子类化并手动启动吗?谢谢.
远离实际加载真实导航堆栈中的视图.真正的UI交互通常需要运行循环来接收事件,因此它们不能在快速单元测试中工作.所以扔掉你的setUp代码.
相反,它自己实例化视图控制器,并加载它:
- (void)testTitleOfSettingsView
{
SettingsViewController *sut = [[SettingsViewController alloc] init];
[sut view]; // Accessing the view causes it to load
XCTAssertEquals(@"Settings", sut.title);
}
Run Code Online (Sandbox Code Playgroud)
另外,了解XCTest中可用的各种断言,而不仅仅是XCAssertTrue.避免在这些断言中发表评论; 小测试中的单个断言应该说明一切.
| 归档时间: |
|
| 查看次数: |
2058 次 |
| 最近记录: |