正在调用委托方法的XCTest测试

Jos*_*ffy 13 macos objective-c ios xctest

我一直在尝试测试我创建的一些类,这些NSNetServer类使用类来执行网络操作.我有一些问题确保调用委托方法.

我尝试了很多方法,包括:

在其他动作发生时使用[NSThread sleepForTimeInterval:5.0f];[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0f]];简单地暂停.该NSRunLoop方法在第一次调用时工作(如下面的示例代码所示),但在第二次调用时崩溃.我理解也不是"正确"的做事方式,但我不知道"正确"的方式是什么.

使用NSConditionNSConditionLock类似乎只是锁定代码和回调从不被调用.

while在回调方法中更改的变量上使用循环,与上面相同.

下面是带有一些额外注释的代码,为简单起见,删除了一些测试:

- (void)testCheckCredentials
{
    [self.server start];
    // Create a client
    self.nsb = [[NSNetServiceBrowser alloc] init];
    // Set the delegate to self
    self.nsb.delegate = self;
    // Search for the server
    [self.nsb searchForServicesOfType:self.protocol inDomain:@""];
    // Wait for the service to be found and resolved
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:self.timeout]];
    XCTAssertTrue(self.serviceWasFound, @"Service was not found");
    // Open the connection to the server
    XCTAssertTrue([self.serverConnection open], @"Connection to server failed to open");
    // Wait for the client to connect
    /* This is where it crashes */
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:self.timeout]];
    XCTAssertTrue(self.clientDidConnect, @"Client did not connect");
    /* Further, more class-specific tests */
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)service moreComing:(BOOL)moreComing
{
    NSLog(@"Found a service: %@ (%@)", service.name, service.domain);
    if ([self.serverName isEqualToString:service.name]) {
        self.serviceWasFound = YES;
    }
}

- (void)clientDidConnect:(RCFClientConnection *)client
{
    XCTAssertNotNil(client, @"Connected client is nil");
    self.clientConnection = client;
    self.clientDidConnect = YES;
}
Run Code Online (Sandbox Code Playgroud)

我也试过lock在一个NSCondition对象上做一个:

[self.nsb searchForServicesOfType:self.protocol inDomain:@""];
// Wait for the service to be found and resolved
[self.lock lockWhenCondition:1];
XCTAssertTrue(self.serviceWasFound, @"Service was not found");
Run Code Online (Sandbox Code Playgroud)

self.serviceWasFound = YES;
[self.lock unlockWithCondition:1]
Run Code Online (Sandbox Code Playgroud)

使用lock方法时,netServiceBrowser:didFindService:moreComing:永远不会调用该方法,当我使用时:

while (!self.serviceWasFound) {};

我还在学习Objective-C,但我只是完全坚持这个问题.

jkr*_*jkr 14

为了处理调用异步执行方法和函数的测试组件,XCTest在Xcode 6中得到了增强,包括使用新API和类XCTestExpectation对象处理块的能力.这些对象响应新的XCTest方法,这些方法允许测试方法等待,直到异步调用返回或达到超时.

以下是上述摘录的Apple文档链接. 编写异步操作测试

@interface sampleAPITests : XCTestCase<APIRequestClassDelegate>{
APIRequestClass *apiRequester;
XCTestExpectation *serverRespondExpectation;
}
@end

//implementation test class
- (void)testAPIConnectivity {
// This is an example of a functional test case.
serverRespondExpectation = [self expectationWithDescription:@"server responded"];
[apiRequester sendAPIRequestForMethod:nil withParams:nil];//send request to server to get tap info
apiRequester.delegate = self;
[self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
    if (error) {
        NSLog(@"Server Timeout Error: %@", error);
    }
   nslog(@"execute here after delegate called  or timeout");
}];
XCTAssert(YES, @"Pass");
}

//Delegate implementation
- (void) request:(APIRequest *)request didReceiveResponse:(NSDictionary *)jsonResponse success:(BOOL)success{
[serverRespondExpectation fulfill];
XCTAssertNotNil(jsonResponse,@"json object returned from server is nil");
}
Run Code Online (Sandbox Code Playgroud)

  • 创建XCTestExpectation是为了解决这个问题.我们自己使用它并且效果很好. (2认同)