我正在使用 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;
}
问题是“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");
}
“testTitleOfMainView”有效。但是“testTitleOfSettingsView 失败,因为 self.settingsViewController 为零。我可以理解为什么。视图尚未初始化。所以我尝试将消息发送到主控制器,将 settignscontroller 置于视图中
[self.tipViewController onSettingsButton];
settingsController 仍然为零。我应该使用模拟吗?有人为我的另一个问题 xctest 提出了这个建议-如何测试是否在按下按钮时加载了新视图
我应该将设置视图子类化并手动启动它吗?谢谢你。