XCTest更改队列

时间:2014-11-12 12:42:45

标签: ios objective-c xcode grand-central-dispatch xctest

是否可以将com.apple.main队列中XCTest中使用的队列更改为其他队列?我在测试同时调度到此队列的应用程序时遇到了麻烦,因此没有进入dispatch_async块。这种现象显然与测试人员阻止使用的队列有关。有没有办法在XCTest测试器中更改“dispatch_get_current_queue”输出?

语言:目标C
XCode:6.0.1
XCTest:在iPad上与Hostapplication进行集成测试 目标操作系统:iOS 7.1和8

1 个答案:

答案 0 :(得分:0)

以下是我如何运行异步单元测试

在单元测试之上添加此宏。

// Macro - Set the flag for block completion
#define StartBlock() __block BOOL waitingForBlock = YES

// Macro - Set the flag to stop the loop
#define EndBlock() waitingForBlock = NO

// Macro - Wait and loop until flag is set
#define WaitUntilBlockCompletes() WaitWhile(waitingForBlock)

// Macro - Wait for condition to be NO/false in blocks and asynchronous calls
// Each test should have its own instance of a BOOL condition because of non-thread safe operations
#define WaitWhile(condition) \
do { \
while(condition) { \
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; \
} \
} while(0)

所以测试可以像这样编写

- (void)testContentNode
{
    StartBlock();
    [AFContentNode loadContentWithUUID:@"d6d87899-94d4-4540-aeab-e88647e4ad61"
                            completion:^(AFMagnoliaNode *node, NSError *error)
     {
         XCTAssert(!error, @"Error raised: %@", error);
         XCTAssert(node, @"Node has not been fetched");
         EndBlock();
     }];
    WaitUntilBlockCompletes();
}