gpt4 book ai didi

ios - 从 View Controller 上的方法内部测试/模拟称为类方法的 UIAlertView

转载 作者:行者123 更新时间:2023-11-28 19:46:13 24 4
gpt4 key购买 nike

我正在为 View Controller 中的 performLogin 方法编写测试,但我无法重构原始方法,也无法重构项目中的任何其他文件。我只能修改我的 LoginTest.m 文件。我正在尝试使用 OCMock 在不更改原始程序文件的情况下完成此测试。

我正在尝试验证在 performLogin 方法收到异常时是否显示 UIAlertView。 ViewHelper 类有一个返回 void 并创建和显示 UIAlertView 的类方法。

我已经模拟了调用 ViewHelper 的 View Controller ,并且我尝试使用 OCMock 使用来自各种搜索的示例来测试 UIAlertView 类,但是这些测试失败了,因为它没有捕捉到 AlertView 的创建。

我是 OCMock 的新手,我不确定如何为此类测试模拟 ViewHelper 文件,尤其是当它是从方法中调用而不是锚定到属性时。

可能有一种简单的方法可以做到这一点,但我还没有弄明白,如果有任何帮助,我们将不胜感激。

提前致谢。

在调用doLogin 时的方法内部,它返回TRUE 或抛出异常。当抛出异常时,它应该弹出一个带有消息的 UIAlertView。我想测试这部分代码并验证 UIAlertView 是否实际显示。

请注意,ViewHelper 是直接从 catch block 中调用的。

方法如下所示:

-(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];

最佳答案

正如 nacho4d 在回答中提到的,这有两个部分:使登录模型抛出异常并验证 Controller 是否显示错误。

考虑到您描述的约束,我会使用登录模型的部分模拟来抛出异常:

id modelMock = OCMPartialMock([[Services sharedInstance] getLoginModel]);
OCMStub([modelMock doLoginWithUsername:[OCMArg any] andPassword:[OCMArg any]])
.andThrow([NSException exceptionWithName:@"TestException" reason:@"Test" userInfo:nil]);

为了检查面板是否显示,我不会理会 View 。我只是检查帮助器中的类方法是否从 performLogin: 实现中调用:

id viewHelperMock = OCMClassMock([ViewHelper class]);
[controller performLogin:sender];
OCMVerify([viewHelperMock showAlertForTitle:"Error" andTheMessage:@"network error" andAccessibilityLabel:@"Network Error"]);

关于ios - 从 View Controller 上的方法内部测试/模拟称为类方法的 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com