iOS writing unit test involving Mknetworkkit

无人久伴 提交于 2019-12-25 03:12:49

问题


I tried to run MKNetworkKit in my project, it seemed to be fine. I then created a unit test project and call the engine to make some requests (using the MkNetworkOperation). I get no apparent error but the completionhandler block never get called, and so was the errorblock.

I examined my unit test setup, there's no apparent error, and the whole thing "ran". I just dont get any response. If i switch and do this in my main project, it works.

I have also added all the necessary framework to the unit test project.

One thing i notice is that Reachability message get printed out for the case that worked, but nothing for the case that didnt.

Any clue as to whats going on?


回答1:


Sounds like you need a semaphore or something similar to block the execution until the request returns. Unit tests run until the end of the method. If they hit the end of the method without an error, they were successful. Since your using MKNetworkKit, the server request is async, so the end of the method is hit before the request is complete. I found a helper class to help with unit tests. I'll try to find it again and link to it with some sample code.

Found it: http://www.touch-code-magazine.com/unit-testing-for-blocks-based-apis/

So it's been a while since I wrote these tests. I'm pretty sure this will work, but you might have to tweak it.

-(void)tests {
    //setup
    NSString *key = @"myTestKey";

    //test
    STAssertNoThrow(
        [API resetPassword:@"fakeemail@this.net callback:^(NSDictionary *result) {

        STAssertNotNil(result, @"reset pass response was nil"]);
        [[TestSemaphor sharedInstance] lift:key];
    } error:^(NSDictionary *error) {

        STFail(@"reset password failed: %@", error.description);
        [[TestSemaphor sharedInstance] lift:key];
    }], @"reset password failed");

    [[TestSemaphor sharedInstance] waitForKey:key];
}


来源:https://stackoverflow.com/questions/17754320/ios-writing-unit-test-involving-mknetworkkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!