我正在尝试模拟一个具有2个块的方法的类(AFHTTPSessionManager
),如何手动触发它们,例如,进行两个测试,一个将使用第一个块实现,另一个测试将使用第二个块实现。
[manager GET:path parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
//First block to trigger.
//logic code here
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//Second block to trigger.
//logic code here
}];
目前,它总是会触发第二个阻止。
我这样嘲笑了这个课:
NSError *error = [NSError errorWithDomain:NSLocalizedFailureReasonErrorKey code:-1009 userInfo:nil];
NSDictionary *responseObject = [NSDictionary dictionaryWithObjectsAndKeys:@"object", @"key", nil];
id mockAFHTTPSessionManager = OCMClassMock([AFHTTPSessionManager class]);
OCMStub([mockAFHTTPSessionManager alloc]).andReturn(mockAFHTTPSessionManager);
OCMStub([mockAFHTTPSessionManager initWithBaseURL:OCMOCK_ANY]).andReturn(mockAFHTTPSessionManager);
OCMStub([mockAFHTTPSessionManager GET:OCMOCK_ANY parameters:OCMOCK_ANY success:([OCMArg invokeBlockWithArgs:OCMOCK_ANY, responseObject, nil]) failure:([OCMArg invokeBlockWithArgs:OCMOCK_ANY, error, nil])]);
答案 0 :(得分:0)
我不知道您是否存在使用alloc / initWithBaseURL的特定原因,但这里我模拟AFHTTPSessionManager:
id mockSessionManager = OCMClassMock([AFHTTPSessionManager class]);
OCMStub([mockSessionManager manager]).andReturn(mockSessionManager);
现在,为了测试成功/失败块调用,我将提出一个让我头疼的建议:不要内嵌你的块。相反,从单独的方法返回块并单独测试这些方法。然后,在测试实际GET:parameters:success:failure
调用的测试中,您只需要预期返回完成块的方法被调用。
以下是一个例子:
<强> MyClass.m 强>
- (void)doGetRequest
{
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
[manager GET:@"http://something.whatever.com"
parameters:nil
success:[self getSuccessBlock]
failure:[self getFailureBlock]];
}
- (void (^)(NSURLSessionDataTask*, id))getSuccessBlock
{
return ^(NSURLSessionDataTask* task, id responseObject)
{
// handle success
};
}
- (void (^)(NSURLSessionDataTask*, NSError*))getFailure
{
return ^(NSURLSessionDataTask* task, NSError* error)
{
// handle failure
}
}
<强> MyClassTests.m 强>
- (void)testDoGetRequest
{
MyClass* myClass = [[MyClass alloc] init];
id mockMyClass = OCMPartialMock(myClass);
id mockSessionManager = OCMClassMock([AFHTTPSessionManager class]);
OCMStub([mockSessionManager manager]).andReturn(mockSessionManager);
id successBlock = ^(NSURLSessionDataTask* task, id o){};
id failureBlock = ^(NSURLSessionDataTask* task, NSError* error){};
OCMExpect([mockMyClass getSuccess]).andReturn(successBlock);
OCMExpect([mockMyClass getFailure]).andReturn(failureBlock);
OCMExpect([mockSessionManager GET:OCMOCK_ANY
parameters:OCMOCK_ANY
success:successBlock
failure:failureBlock]);
[myClass doGetRequest];
OCMVerifyAll(mockMyClass);
OCMVerifyAll(mockSessionManager);
}
- (void)testGetSuccessBlock
{
MyClass* myClass = [[MyClass alloc] init];
// setup expectations
NSURLSessionDataTaks* fakeTask = // whatever you need here
id fakeResponseObject = // whatever you need here
[myClass getSuccessBlock](fakeTask, fakeResponseObject);
// evaluate expectations
}
- (void)testGetFailureBlock
{
MyClass* myClass = [[MyClass alloc] init];
// setup expectations
NSURLSessionDataTaks* fakeTask = // whatever you need here
NSError* fakeError = // whatever you need here
[myClass getFailureBlock](fakeTask, fakeError);
// evaluate expectations
}
(我写的这个基本上是我的头脑,所以一些语法可能不是100%,但你应该得到这个想法)