Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async test, why codes in callback block cannot be called? #154

Closed
Banzuofan opened this issue Jul 2, 2015 · 3 comments
Closed

Async test, why codes in callback block cannot be called? #154

Banzuofan opened this issue Jul 2, 2015 · 3 comments

Comments

@Banzuofan
Copy link

I am writing async test codes as the guide at https://github.com/specta/expecta, but i met a question, when run the test method, it seems that codes in callback block didn't run, i don't know what happen to my code, or something i missed. my code as follow:

- (void)testAsyncNetworkTask
{
    //XCTestExpectation *expecta = [self expectationWithDescription:@"asyncTask_login"];
    [Expecta setAsynchronousTestTimeout:15.0];

    __block id reponseData = nil;
    __block id error = nil;
    NSURLRequest *_request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    [NSURLConnection sendAsynchronousRequest:_request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSLog(@"%@", reponseData);
        reponseData = data;
        error = connectionError;

        //[expecta fulfill];
    }];

    XCTAssertNil(reponseData);
    expect(reponseData).willNot.beNil;
    expect(error).will.beNil;

//    [self waitForExpectationsWithTimeout:15 handler:^(NSError *error) {
//        //NSLog(@"%s, time out", __FUNCTION__);
//    }];
}

When I used XCTest to test my codes, it can run the codes in callback block, and i can see logs in console. I refered to AFNetworking demo containing expecta when i wrote my test codes.
Hope you can understand what I said!

@orta
Copy link
Member

orta commented Jul 2, 2015

Your test structure may work better this way using waitUntil

@StatusReport
Copy link
Contributor

You're forgetting the parenthesis after the beNil (yes, it's troubling that the code compiles without them), so your spec probably terminates early before the callback is being called.

Try with:

expect(reponseData).willNot.beNil();
expect(error).will.beNil();

@Banzuofan
Copy link
Author

Thanks for your replies, @StatusReport it works as your codes, i feel so shame for my carelessness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants