简体   繁体   English

Xcode中的XCTest和异步测试

[英]XCTest and asynchronous testing in Xcode 6

So Apple said in the release note of Xcode 6 that we can now do asynchronous testing directly with XCTest. 所以Apple在Xcode 6的发行说明中说我们现在可以直接使用XCTest进行异步测试。

Anyone knows how to do it using Xcode 6 Beta 3 (Using objective-C or Swift)? 任何人都知道如何使用Xcode 6 Beta 3(使用Objective-C或Swift)? I don't want the known semaphore method, but the new Apple way. 我不想要已知的信号量方法,而是新的Apple方式。

I searched into the released note and more but I found nothing. 我搜索了发布的笔记和更多,但我一无所获。 The XCTest header is not very explicit either. XCTest标头也不是非常明确。

Obj-C example: Obj-C示例:

- (void)testAsyncMethod
{

    //Expectation
    XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method Works!"];

    [MyClass asyncMethodWithCompletionBlock:^(NSError *error, NSHTTPURLResponse *httpResponse, NSData *data) {

        if(error)
        {
            NSLog(@"error is: %@", error);
        }else{
            NSInteger statusCode = [httpResponse statusCode];
            XCTAssertEqual(statusCode, 200);
            [expectation fulfill];
        }

    }];


    [self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {

        if(error)
        {
            XCTFail(@"Expectation Failed with error: %@", error);
        }

    }];
}

The sessions video is perfect, basically you want to do something like this 会话视频是完美的,基本上你想做这样的事情

func testFetchNews() {
    let expectation = self.expectationWithDescription("fetch posts")

    Post.fetch(.Top, completion: {(posts: [Post]!, error: Fetcher.ResponseError!) in
        XCTAssert(true, "Pass")
        expectation.fulfill()
    })

    self.waitForExpectationsWithTimeout(5.0, handler: nil)
}

Session 414 covers async testing in Xcode6 会话414涵盖了Xcode6中的异步测试

https://developer.apple.com/videos/wwdc/2014/#414 https://developer.apple.com/videos/wwdc/2014/#414

How I did in swift2 我是怎么做的swift2

Step 1: define expectation 第1步:定义期望

let expectation = self.expectationWithDescription("get result bla bla")

Step 2: tell the test to fulfill expectation right below where you capture response 第2步:告诉测试在下面捕获响应的地方满足期望

responseThatIGotFromAsyncRequest = response.result.value
expectation.fulfill()

Step 3: Tell the test to wait till the expectation is fulfilled 第3步:告诉测试等到期望满足

waitForExpectationsWithTimeout(10)

STep 4: make assertion after async call is finished STep 4:在异步调用完成后进行断言

XCTAssertEqual(responseThatIGotFromAsyncRequest, expectedResponse)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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