1

I am using iPhone Sen testing framework for testing. I need to test the asynchronous responses coming from server. Can anyone help me to find out the way to test it.

Thank you

1

3 Answers 3

3

This may have been a duplicate question, but I don't agree with the answer to the original one. I've described why in length on that question.

In summary, creating a custom run loop for your tests is complicated and fragile. Your tests will run more consistently and more quickly if you remove the dependency on the external service that is returning the asynchronous responses and mimic the responses in your tests. This way your tests won't fail if the external service goes down, and your tests will run much more quickly.

There are libraries available that reopen classes like NSURLConnection and make this easy to do. It's also relatively simple to do for a single class (like NSURLConnection), thanks to the dynamic properties of Objective C. Here is an example.

2
  • HI Adam, I just read this post and your other post and agree with what you are saying about testing. By using an external server, it changes the test from being a test of the class, to being an integration test which is a different thing.
    – drekka
    Commented Nov 14, 2010 at 23:05
  • But, Drekka, what if you want an integration test? Commented Jan 26, 2013 at 13:28
1

I don't use Sen test because it doesn't run in a simulator or device (or at least didn't when I first started doing Objective C last year), instead I use GHUnit as a testing framework and OCMock as a mocking framework. GHUnit will run tests on the device which ensures that the code is correct for it. OCMock allows full mocking, partial mocking and method swizzling which are powerful tools for unit a test.

I think there are two parts to testing in this situation.

  1. Unit testing the individual components (GHUnit and OCMock). Here you can call methods direct and simulate the environment you class will run in. So you don't need a server setup to test async calls. Just instantiate the class in a unit test and call it's methods.

  2. Integration testing. (GHUnit only) Here you want to do a basic test to ensure that your understanding of the communication between your code and the server is correct. So this is where you do need a server. Depending on your needs, this may be a lot or a little amount of work. For example when I've done this I've just setup a glassfish server with an EJB3 bean to simulate the server I've going to connect to. The important thing in this is that the server you setup must behave exactly the same way as the final production server.

So basically it's a two pronged approach. Unit testing to check every nook and cranny of your code. Integration testing to ensure that the overall communication with the server is being handled correctly.

Using both is important because whilst unit testing can check functionality that is difficult to check with an integration test, it is only as accurate as your understanding of the communication between the class and server. So you need at least a basic integration test to confirm your understanding is correct. But don't waste time writing lots of integration tests to check every nook and cranny of the class. It's always much harder than a unit test.

2
  • Sounds good. Looking forward to user this approaches. Other thing I faced during tests is the time consumption. When I test that using NSRunLoop it takes lot of time. So is there any way to process test case parallelly in your approaches?
    – Dilshan
    Commented Nov 15, 2010 at 6:38
  • GHUnit does run tests as a background task and you can switch them to the main thread if necessary (UI Stuff for example). I also found a blog posting talking about the amount of time saved by running on the main thread instead of a background one due to the time it takes to start up a new thread: (jorudolph.wordpress.com/2009/10/23/…). GHUnit can also work with SenTest test. As for the run loops. With mocking from OCMock you may be able to avoid having to use the runloop. Just a thought.
    – drekka
    Commented Nov 16, 2010 at 2:49
0

I use this for testing async operations.

https://github.com/hfossli/AGWaitForAsyncTestHelper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.