首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对被调用的委托方法进行XCTest测试

对被调用的委托方法进行XCTest测试
EN

Stack Overflow用户
提问于 2014-06-01 00:01:40
回答 2查看 10.1K关注 0票数 13

我一直在尝试测试我创建的一些类,这些类使用NSNetServer类等执行网络操作。我在确保委托方法被调用时遇到了一些问题。

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

使用[NSThread sleepForTimeInterval:5.0f];[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0f]];在其他操作发生时简单地暂停。NSRunLoop方法在第一次调用时可以正常工作(如下面的示例代码所示),但在第二次调用时会崩溃。我知道这两种方法都不是“正确”的,但我不知道什么是“正确”的方法。

使用NSConditionNSConditionLock类,这似乎只是锁定了代码,并且回调永远不会被调用。

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

为了简单起见,下面的代码包含了一些额外的注释和一些删除的测试:

代码语言:javascript
运行
复制
- (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

代码语言:javascript
运行
复制
[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");

代码语言:javascript
运行
复制
self.serviceWasFound = YES;
[self.lock unlockWithCondition:1]

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

while (!self.serviceWasFound) {};

我还在学习Objective-C,但我完全被这个问题卡住了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-28 16:58:10

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

这是上面摘录的苹果文档的链接。Writing Tests of Asynchronous Operations

代码语言:javascript
运行
复制
@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");
}
票数 15
EN

Stack Overflow用户

发布于 2014-06-01 10:07:28

许多测试库支持测试异步和线程化操作。

它们基本上都以相同的方式工作:

在第一个条件之后,等待一个条件occur.

  • Optionally执行一组断言,等待x秒,如果其中一个条件不满足,则失败。如果所需条件未在指定(或默认)时间内出现,则

  • 将失败。

以下是一些提供这些功能的库:

  • expecta匹配库。
  • Kiwi测试框架。
  • Typhoon DI框架有一个用于执行异步集成测试的实用程序。

据我所知,所有这些库都使用运行循环方法,因为其他库可能会导致死锁。要让这种方法可靠地工作:

  • 在测试基于块的回调时,您可以内联创建块。
  • 对于委托回调,您应该创建一个单独的存根(协议的最简单实现)或使用模拟(使用模拟库创建的“魔术”实现),并将其设置为委托。如果将测试实例本身设置为委托,则可能会出现问题。

编辑:

在Xcode6中,有一个用于异步测试的新断言XCTestExpectation

以上每个库都可以使用CocoaPods进行安装。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23972019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档