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;
}

我也尝试过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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法调用委托方法

来自分类Dev

未调用委托方法

来自分类Dev

在 Func 委托中调用 Func 委托的方法

来自分类Dev

委托多个方法的方法调用

来自分类Dev

Elixir中的测试方法委托

来自分类Dev

如何对iOS应用程序委托方法的调用进行单元测试?

来自分类Dev

NSFetchedResultsControllerDelegate委托方法未调用

来自分类Dev

UIPageViewController委托方法未调用

来自分类Dev

委托方法没有被调用?

来自分类Dev

MFMailComposeViewController委托方法未调用

来自分类Dev

未调用NSURLSession委托方法

来自分类Dev

UITextField委托方法未调用

来自分类Dev

不再调用FBLoginView委托方法

来自分类Dev

XMLParser不调用委托方法

来自分类Dev

NSSplitViewController不调用委托方法

来自分类Dev

MFMailComposeViewController委托方法未调用

来自分类Dev

UIPageViewController委托方法未调用

来自分类Dev

未调用NSSplitView委托方法

来自分类Dev

委托方法未调用-iOS

来自分类Dev

UIImagePickerController不调用委托方法

来自分类Dev

如何调用UIWebview的委托方法?

来自分类Dev

未调用类委托方法

来自分类Dev

未调用 XMLParser 委托方法

来自分类Dev

反射方法调用与委托调用的性能

来自分类Dev

反射方法调用与委托调用的性能

来自分类Dev

如何从SubClass委托方法调用SuperClass委托方法

来自分类Dev

在私有委托方法中测试异常

来自分类Dev

不调用委托方法,将委托设置为self?

来自分类Dev

在原始子类中捕获委托方法调用