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

Use XCTestExpectation #590

Closed
fabb opened this issue Dec 4, 2014 · 5 comments
Closed

Use XCTestExpectation #590

fabb opened this issue Dec 4, 2014 · 5 comments

Comments

@fabb
Copy link

fabb commented Dec 4, 2014

Using XCTestExpectation makes testing of async functionality possible. There is shouldEventually in Kiwi, but it is not as flexible as XCTestExpectation.
To create a new promise with XCTestExpectation it is necessary to access self which is not possible in Kiwi AFAIK. Any ideas, workarounds.

At the moment I use RXPromise as a workaround, with its runLoopWait method I can wait for the promise to be fulfilled/rejected in a test case. I'd prefer to use XCTestExpectation though.

@fatuhoku
Copy link

👍

@sharplet
Copy link
Contributor

Would you mind explaining why you feel that expectFutureValue/shouldEventually is not flexible enough?

@fabb
Copy link
Author

fabb commented May 15, 2015

Kiwi's shouldEventually polls for the expected value until timeout. In several cases, I want to execute something asynchronously, and when finished (successfully or not), I want to do several checks on it. And if the asynchronous task fails, I want the unit test to fail fast and not wait until timeout.

Consider this example borrowed from NSHipster:

it(@"async test", ^{
    NSURL *URL = [NSURL URLWithString:@"http://nshipster.com/"];
    NSString *description = [NSString stringWithFormat:@"GET %@", URL];
    XCTestExpectation *expectation = [self expectationWithDescription:description];

    NSURLSession *session = [NSURLSession sharedSession];
    __block NSData *pData = nil;
    __block NSURLResponse *pResponse = nil;
    __block NSError *pError = nil;
    NSURLSessionDataTask *task = [session dataTaskWithURL:URL
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                            pData = data;
                                            pResponse = response;
                                            pError = error;
                                            [expectation fulfill];
                                        }];

    [task resume];

    [self waitForExpectationsWithTimeout:task.originalRequest.timeoutInterval handler:^(NSError *error) {
        [task cancel];
        [[pError should] beNil];
        [[pData shouldNot] beNil];
        [[pResponse shouldNot] beNil];
        [[pResponse should] beKindOfClass:[NSHTTPURLResponse class]];
        [[theValue([(NSHTTPURLResponse *)pResponse statusCode]) should] equal:theValue(200)];
        [[pResponse.URL.absoluteString should] equal:URL.absoluteString];
        [[pResponse.MIMEType should] equal:@"text/html"];
    }];
});

@tsabend
Copy link

tsabend commented Jun 17, 2015

+1 Running into problems with Kiwi + OHHTPPStub https://github.com/AliSoftware/OHHTTPStubs/wiki/OHHTTPStubs-and-asynchronous-tests

@modocache
Copy link
Member

Thanks for the idea, @fabb! This would be cool, and has been discussed on other testing/expectation frameworks: Quick/Nimble#28. My thoughts, echoed in that pull request, is that XCTestExpectation has too many restrictions -- it requires the current XCTestCase to vend expectations, it has very specific usages in mind that don't play well with dynamically generated tests, and so on.

Closing this for now -- we probably don't have the resources to really explore this idea further, even if it was something we wanted.

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

5 participants