简体   繁体   English

使用OCunit在iOS上测试异步代码

[英]Testing asynchronous code on iOS with OCunit

Does anyone know of any existing library that allows testing asynchronous code with OCUnit? 有没有人知道任何允许使用OCUnit测试异步代码的现有库? I'm thinking about something like GHAsyncTestCase but that delivers from SenTestCase . 我正在考虑像GHAsyncTestCase这样的GHAsyncTestCase但是它来自SenTestCase

I'm asking because if I don't find any I'm going to port GHAsyncTestCase to OCUnit but I don't want to duplicate work done already by someone else. 我问,因为如果我找不到任何我要将GHAsyncTestCase到OCUnit,但我不想复制其他人已完成的工作。

Piotr: 彼得:

You might be interested in the work I've done with Mimic , a network stubbing tool written in Ruby which lets you stub requests at a high-level and can be used from your Objective-C using the supplied wrapper. 您可能对我使用Mimic所做的工作感兴趣, Mimic是一个用Ruby编写的网络存根工具,它允许您在高级别存根请求,并且可以使用提供的包装器从Objective-C中使用。

With regards to asynchronous testing, please take a look at a little utility I wrote called AssertEventually . 关于异步测试,请看一下我写的一个名为AssertEventually的小实用程序。

This example shows both Mimic and assertEventually in action. 此示例显示Mimic和assertEventually正在执行。

In addition, you might want to look at Kiwi - it's a great little Objective-C testing framework that is built on top of OCUnit. 另外,你可能想看一下Kiwi - 它是一个很好的小型Objective-C测试框架,它建立在OCUnit之上。 I recently contributed a patch to port my AssertEventually behaviour over to Kiwi which lets you write things like: 我最近贡献了一个补丁来将我的AssertEventually行为移植到Kiwi,它可以让你写下这样的东西:

id someObject = nil;
[do SomethingThatFetchesSomeObjectAsynchronously];
[[theObject(&someObject) shouldEventually] equal:@"some result"];

AGAsyncTestHelper takes a slightly different approach than AssertEventually since it is evaluating the expression rather than checking the pointer for new results. AGAsyncTestHelper采用与AssertEventually略有不同的方法,因为它正在评估表达式而不是检查指针是否有新结果。 One advantage of using AGAsyncTestHelper is that it can be used for blocks, delegate-callbacks and whatnot. 使用AGAsyncTestHelper的一个优点是它可以用于块,委托 - 回调等等。

WAIT_WHILE(<expression_to_evaluate>, <max_duration>);

Answering the question 回答这个问题

id someObject = nil;
[self doSomethingThatFetchesSomeObjectAsynchronously];
WAIT_WHILE(self.someObject == nil, 1.0); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM