简体   繁体   中英

OCMock stub method with two block completion

I'm trying to mock a class ( AFHTTPSessionManager ) that has a method with 2 blocks, how can I trigger them manually, like, making two test that one will use the first block implementation and the other test will use the second block implementation.

[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
}];

Currently, it will always trigger the second block.

I've mocked the class like this:

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

I don't know if there's a specific reason you're stubbing alloc / initWithBaseURL, but here's how I mock AFHTTPSessionManager:

id mockSessionManager = OCMClassMock([AFHTTPSessionManager class]);
OCMStub([mockSessionManager manager]).andReturn(mockSessionManager);

Now, for testing the success / failure block invocations, I'm going to make a suggestion that saved me a bunch of headaches: don't inline your blocks. Instead, return the blocks from a separate method and test those methods individually. Then, in the test that is testing the actual GET:parameters:success:failure call, you just need to expect that the methods that return your completion blocks are called.

Here's an example:

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
}

(I wrote this basically off the top of my head, so some of the syntax might not be 100%, but you should get the idea)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM