大佬教程收集整理的这篇文章主要介绍了ios – 测试是否在视图控制器方法中调用performSegueWithIdentifier,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经能够毫无问题地测试所有常用的返回方法.但是我目前难以接受我想要执行的某项测试:
- (IBACtion)doLogin:(UIButton *)sender { // Some logic here if ( //certain criteria to meet) { variable = x; // important variable set here [self performSegueWithIdentifier:@"memorableWord" sender:sender]; } else { // handler error here }
所以我想测试是否调用segue并设置变量,或者加载MemorableWord视图控制器并且其中的变量是正确的.在doLogin方法中设置的变量将传递到prepareForSegue方法中的memorableWord segues’目标视图控制器.
我有OCmock设置和工作,我也使用XCTest作为我的单元测试框架.有没有人能够进行单元测试以涵盖这种情况?
对于这个领域的信息来说,Google和SO似乎相当简陋.很多关于简单基本测试的例子与iOS测试中更复杂的现实无关.
>点击登录按钮时,将使用loginButton作为发件人调用doLogin
>如果某些条件为YES,请调用performSegue
因此,您实际上应该从登录按钮触发完整流程以执行Sese:
- (void)testLogin { LoginViewController *loginViewController = ...; id loginmock = [OCmockObject partialmockForObject:loginViewController]; //here the expect call has the advantage of swallowing performSegueWithIdentifier,you can use forWARDToRealObject to get it to go all the way through if necessary [[loginmock expect] performSegueWithIdentifier:@"memorableWord" sender:loginViewController.loginButton]; //you also expect this action to be called [[loginmock expect] doLogin:loginViewController.loginButton]; //mocking out the criteria to get through the if statement can happen on the partial mock as well BOOL doSegue = YES; [[[loginmock expect] andReturnValue:OCmock_VALUE(doSeguE)] criteria]; [loginViewController.loginButton sendActionsForControlEvents:UIControlEventTouchUpInside]; [loginmock verify]; [loginmock stopmocking]; }
您需要为“条件”实现一个属性,以便有一个可以使用’expect’模拟的getter.
重要的是要意识到“期望”只会模拟1次调用getter,后续调用将失败并显示“调用意外方法…”.您可以使用’stub’来模拟所有调用,但这意味着它将始终返回相同的值.
以上是大佬教程为你收集整理的ios – 测试是否在视图控制器方法中调用performSegueWithIdentifier全部内容,希望文章能够帮你解决ios – 测试是否在视图控制器方法中调用performSegueWithIdentifier所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。