[英]Test view title using XCTest
I am using XCtest to test the title of a view. 我正在使用XCtest来测试视图的标题。 Trying to get into the habit of writing tests first.
尝试养成先编写测试的习惯。 Setup looks like
安装程序看起来像
- (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;
}
The problem is "settingsViewController". 问题是“ settingsViewController”。 I have two functions for the actual test:
我有两个用于实际测试的功能:
- (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");
}
The "testTitleOfMainView" works. “ testTitleOfMainView”起作用。 But the "testTitleOfSettingsView fails as self.settingsViewController is nil. I can sort of understand why. The view has not been initialized as yet. So I tried sending the message to the main controller that brings the settignscontroller in view
但是“ testTitleOfSettingsView失败,因为self.settingsViewController为nil。我可以理解为什么。该视图尚未初始化。因此,我尝试将消息发送到主控制器,从而使settignscontroller进入视图
[self.tipViewController onSettingsButton];
The settingsController is still nil. settingsController仍然为零。 Should I be using mocks?
我应该使用模拟吗? Somebody suggested this for my other question xctest - how to test if a new view loads on a button press
有人为我的其他问题xctest提出了建议-如何测试按钮按下时是否加载了新视图
Should I subclass the settingsview and bring it up manually? 是否应该将settingsview子类化并手动调出? Thank you.
谢谢。
Stay away from actually loading views in a real navigation stack. 远离在实际的导航堆栈中实际加载视图。 Real UI interactions typically need the run loop to receive events, so they will not work in a fast unit test.
实际的UI交互通常需要运行循环来接收事件,因此它们无法在快速的单元测试中起作用。 So throw away your setUp code.
因此,丢弃您的setUp代码。
Instead, instantiate the view controller on its own, and have it load: 取而代之的是,自己实例化视图控制器,并加载它:
- (void)testTitleOfSettingsView
{
SettingsViewController *sut = [[SettingsViewController alloc] init];
[sut view]; // Accessing the view causes it to load
XCTAssertEquals(@"Settings", sut.title);
}
Also, learn the various assertions that are available to you in XCTest, not just XCAssertTrue. 另外,了解XCTest中可用的各种断言,而不仅仅是XCAssertTrue。 Avoid comments in those assertions;
避免在这些断言中发表评论; a single assertion in a small test should speak for itself.
小型测试中的单个断言应该说明一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.