gpt4 book ai didi

ios - OCMock 和 block 测试,执行

转载 作者:技术小花猫 更新时间:2023-10-29 10:45:08 24 4
gpt4 key购买 nike

下面是被测方法:

- (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass {

NSDictionary *userPassD = @{@"user":userName,
@"pass":pass};
[_loginCntrl loginWithUserPass:userPassD withSuccess:^(NSString *authToken){
// save authToken to credential store
} failure:^(NSString *errorMessage) {
// alert user pass was wrong
}];
}

我想测试的是,在那个成功 block 中,其他依赖项/OCMockObject _credStore 是用适当的方法调用的。所以目前 loginCtrl 和 credStore 依赖项是 OCMockObjects,我可以 stub /期望它们。

我会 stub loginController 以在调用时以某种方式执行该 block 吗?我已经查看了一些关于使用 OCMock stub block 的问题,但我无法理解他们在做什么以及它是否适合这种情况。

实际上我想做的就是 OCMock 触发 block ([成功调用]??)以便代码 _credStore saveUserPass 完成并可以在 _credStore 上验证。

我停下来的地方:

- (void)test_loginWithuserPass_succeeds_should_call_credStore_setAuthToken {

NSDictionary *userPassD = @{@"user":@"mark",
@"pass":@"test"};
id successBlock = ^ {
// ??? isn't this done in the SUT?
};

[[[_loginController stub] andDo:successBlock] loginWithUserPass:userPassD withSuccess:OCMOCK_ANY failure:OCMOCK_ANY];
[[_credentialStore expect] setAuthToken:@"passed back value from block"];
[_docServiceSUT loginWithUser:@"mark" andPass:@"test"];
[_credentialStore verify];
}

预计到达时间:这是我根据下面 Ben 的示例得出的结果,但无法正常工作,出现 EXC_BAD_ACCESS 异常:

// OCUnit test method
- (void)test_loginWithUserPass_success_block_should_call_credentials_setAuthToken {

void (^proxyBlock)(NSInvocation*) = ^(NSInvocation *invocation) {
void(^successBlock)(NSString *authToken);
[invocation getArgument:&successBlock atIndex:3]; // should be 3 because my block is the second param
successBlock(@"myAuthToken");
};

[[[_loginController expect] andDo:proxyBlock] loginWithUserPass:OCMOCK_ANY withSuccess:OCMOCK_ANY failure:OCMOCK_ANY];
[[_credentialStore expect] setAuthToken:@"myAuthToken"];
[_docServiceSUT loginWithUser:@"mark" andPass:@"myPass"];
[_loginController verify];
[_credentialStore verify];
}

//method under test
- (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass {

NSDictionary *userPassD = @{@"user":userName,
@"pass":pass};

void(^onSuccess)(NSString *) = ^(NSString *authToken){

[SVProgressHUD dismiss];
[_credentials setAuthToken:authToken];

// Ask user to enter the 6 digit authenticator key
[self askUserForAuthenticatorKey];
};

void(^onFailure)(NSString *) = ^(NSString *errorMessage) {

[SVProgressHUD dismiss];
[_alertSender sendAlertWithMessage:errorMessage andTitle:@"Login failed"];
};

[SVProgressHUD show];
[_loginCntrl loginWithUserPass:userPassD withSuccess:onSuccess
failure:onFailure];
}

最佳答案

如果我没听错,这可能会如你所愿:

@interface ExampleLC : NSObject
- (void)loginWithUserPass:userPassD withSuccess:(void (^)(NSString *authToken))successBlock failure:(void (^)(NSString *errorMessage))failureBlock;
@end
@implementation ExampleLC
- (void)loginWithUserPass:userPassD withSuccess:(void (^)(NSString *authToken))successBlock failure:(void (^)(NSString *errorMessage))failureBlock
{
}
@end
@interface Example : NSObject {
@public
ExampleLC *_loginCntrl;
}
- (void)saveToken:(NSString *)authToken;
- (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass;
@end
@implementation Example
- (void)saveToken:(NSString *)authToken
{
}
- (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass {

NSDictionary *userPassD = @{@"user":userName,
@"pass":pass};
[_loginCntrl loginWithUserPass:userPassD withSuccess:^(NSString *authToken){
// save authToken to credential store
[self saveToken:authToken];
} failure:^(NSString *errorMessage) {
// alert user pass was wrong
}];
}
@end


@interface loginTest : SenTestCase

@end

@implementation loginTest

- (void)testExample
{
Example *exampleOrig = [[Example alloc] init];
id loginCntrl = [OCMockObject mockForClass:[ExampleLC class]];
[[[loginCntrl expect] andDo:^(NSInvocation *invocation) {
void (^successBlock)(NSString *authToken) = [invocation getArgumentAtIndexAsObject:3];
successBlock(@"Dummy");
}] loginWithUserPass:OCMOCK_ANY withSuccess:OCMOCK_ANY failure:OCMOCK_ANY];
exampleOrig->_loginCntrl = loginCntrl;
id example = [OCMockObject partialMockForObject:exampleOrig];
[[example expect] saveToken:@"Dummy"];
[example loginWithUser:@"ABC" andPass:@"DEF"];
[loginCntrl verify];
[example verify];
}
@end

此代码允许使用您指定的参数调用真正的成功 block ,然后您可以验证该参数。

关于ios - OCMock 和 block 测试,执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17644772/

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