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)? I don't want the known semaphore method, but the new Apple way.
任何人都知道如何使用Xcode 6 Beta 3(使用Objective-C或Swift)?我不想要已知的信号量方法,而是新的Apple方式。
I searched into the released note and more but I found nothing. The XCTest header is not very explicit either.
我搜索了发布的音符以及更多,但我一无所获。 XCTest标头也不是很明确。
4 个解决方案
#1
63
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);
}
}];
}
#2
50
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)
}
#3
11
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
#4
1
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)
#1
63
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);
}
}];
}
#2
50
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)
}
#3
11
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
#4
1
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)