我正在从视图控制器中编写performLogin方法的测试,我无法重构原始方法,也无法重构原始方法中的任何其他文件项目。我只能修改我的LoginTest.m文件。我正在尝试使用OCMock来完成此测试,而不更改原始程序文件。从视图控制器的方法内部调用UIAlertView作为类方法的测试/嘲弄
我想验证,如果performLogin方法收到异常显示UIAlertView。 ViewHelper类有一个返回void的类方法,并创建并显示一个UIAlertView。
我嘲笑调用ViewHelper的视图控制器,并试图使用OCMock使用来自各种搜索的示例测试UIAlertView类,但是由于它不捕获AlertView的创建,所以这些测试失败。
我是新的使用OCMock,我不知道如何模拟这种类型的测试ViewHelper文件,特别是它从方法内调用,而不是锚定到属性。
有可能是一个简单的方法来做到这一点,但我还没有弄明白,任何帮助将不胜感激。
在此先感谢。
在调用doLogin的方法内部,它返回TRUE或引发异常。当抛出异常时,它应该弹出一个带有消息的UIAlertView。我想测试代码的这一部分并确认UIAlertView实际显示。
请注意,ViewHelper是直接从catch块中调用的。
的方法是这样的:
-(IBAction)performLogin:(id)sender {
if(valid)
{
@try
{
//loginModel is a private instance variable
loginModel = [[Services sharedInstance] getLoginModel];
[loginModel doLoginWithUsername:username andPassword:password];
[self performSegueWithIdentifier:@"showWelcomeScreen" sender:self];
}
@catch(NSException *e)
{
//I NEED TO TEST THAT THIS CALL OCCURS AND THE UIAlertView is shown
//Not sure how to do this as it's not anchored to a property or method
//and is called as a class method
[ViewHelper showAlertForTitle:"Error" andTheMessage:@"network error"
andAccessibilityLabel:@"Network Error"];
}
}
}
的ViewHelpder.m看起来是这样的 - 请注意,它的一类方法:
+(void) showAlertForTitle:(NSString *)title andTheMessage:(NSString *)message andAccessibilityLabel:(NSString *)label
{
NSLog(@"reached alert notification class");
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[successAlert setAccessibilityLabel:label];
[successAlert show];
}
别的不说是我到目前为止已经试过:
- (void)testForfailedLogin {
//call singleton first to initialize mocked login model
[[Services sharedInstance] getLoginModel];
id mockAlertView = [OCMockObject mockForClass:[UIAlertView class]];
[[[mockAlertView stub] andReturn:mockAlertView] alloc];
(void)[[[mockAlertView expect] andReturn:mockAlertView]
initWithTitle:OCMOCK_ANY
message:OCMOCK_ANY
delegate:OCMOCK_ANY
cancelButtonTitle:OCMOCK_ANY
otherButtonTitles:OCMOCK_ANY, nil];
[[mockAlertView expect] show];
//set the actual IBOutlets with login values designed to sucessfully login
self.controller.usernameTextField.text = @"error";
self.controller.passwordTextField.text = @"error";
//call the login process to verify the login method
[self.controller performLogin:nil];
[mockAlertView verify];
[mockAlertView stopMocking];
}
感谢您的帮助,就引发我需要什么来解决我的问题。 – CocoaEv