我一直在尝试测试我创建的一些类,这些类使用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;
}
我还尝试过在NSCondition
对象上执行lock
:
[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,但我完全被这个问题卡住了。
发布于 2015-04-28 08:58:10
为了处理调用异步执行方法和函数的测试组件,XCTest在Xcode6中得到了增强,包括使用新的API和类XCTestExpectation的对象处理块的能力。这些对象响应新的XCTest方法,这些方法允许测试方法等待,直到异步调用返回或超时。
这是上面摘录的苹果文档的链接。Writing Tests of Asynchronous Operations
@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");
}
发布于 2014-06-01 02:07:28
许多测试库支持测试异步和线程化操作。
它们基本上都以相同的方式工作:
在第一个条件之后,等待一个条件occur.
以下是一些提供这些功能的库:
据我所知,所有这些库都使用运行循环方法,因为其他库可能会导致死锁。要让这种方法可靠地工作:
编辑:
在Xcode6中,有一个用于异步测试的新断言XCTestExpectation
。
以上每个库都可以使用CocoaPods进行安装。
https://stackoverflow.com/questions/23972019
复制相似问题