我一直在尝试测试我创建的某些类,这些NSNetServer
类使用该类执行网络操作。我在确保调用委托方法时遇到一些问题。
我尝试了许多方法,包括:
在其他动作发生时,使用[NSThread sleepForTimeInterval:5.0f];
和[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0f]];
可以简单地暂停。该NSRunLoop
方法在第一次调用时有效(如下面的示例代码所示),但在第二次调用时崩溃。我了解这都不是“正确”的处理方式,但我不知道“正确”的处理方式是什么。
使用NSCondition
和NSConditionLock
类,似乎只是锁定了代码,并且从未调用过回调。
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;
}
我也尝试过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");
和
self.serviceWasFound = YES;
[self.lock unlockWithCondition:1]
使用lock方法时,netServiceBrowser:didFindService:moreComing:
永远不会调用该方法,与我使用时相同:
while (!self.serviceWasFound) {};
我仍在学习Objective-C,但我完全陷入了这个问题。
为了处理调用异步执行方法和函数的测试组件,Xcode 6中对XCTest进行了增强,使其具有使用新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");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句