Testing asynchronous code on iOS with OCunit
Asked Answered
S

2

7

Does anyone know of any existing library that allows testing asynchronous code with OCUnit? I'm thinking about something like GHAsyncTestCase but that delivers from 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.

Snarl answered 13/1, 2011 at 21:38 Comment(2)
I don't know of anyone doing this before but I have to ask; why do you feel the need to test asynchronous behavior in a framework normally used to run unit tests? Do you not trust the two halves of your behavior? Can you not test them separately? Are you really writing integration tests?Egomania
@Jonah, You are right it is really useful but sometimes I'm too lazy to mock up the whole Network Layer. Then it is really tempting to just run a local server and integration tests against it.Snarl
T
11

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.

With regards to asynchronous testing, please take a look at a little utility I wrote called AssertEventually.

This example shows both Mimic and assertEventually in action.

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. I recently contributed a patch to port my AssertEventually behaviour over to Kiwi which lets you write things like:

id someObject = nil;
[do SomethingThatFetchesSomeObjectAsynchronously];
[[theObject(&someObject) shouldEventually] equal:@"some result"];
Trondheim answered 24/1, 2011 at 16:17 Comment(3)
It is something that I was looking for. Mimic looks quite interesting I will have a look at it. Thank you!Snarl
AssertEventually sounds handy, but the link is dead. Any help?Reverse
I'm using Kiwi with the -shouldEventually behavior; fancy to find that it was your handiwork. Thanks!Analphabetic
L
7

AGAsyncTestHelper takes a slightly different approach than AssertEventually since it is evaluating the expression rather than checking the pointer for new results. One advantage of using AGAsyncTestHelper is that it can be used for blocks, delegate-callbacks and whatnot.

WAIT_WHILE(<expression_to_evaluate>, <max_duration>);

Answering the question

id someObject = nil;
[self doSomethingThatFetchesSomeObjectAsynchronously];
WAIT_WHILE(self.someObject == nil, 1.0); 
Loaiasis answered 26/7, 2013 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.