TDD: Testing Module Asynchronous Calls

guys:

I am working on an application and building it with unit testing.

However, I am now in a situation where I need to test asynchronous calls. For instance,

- (void)testUserInfoBecomesValidWhenUserIsBuiltSuccessfully { if ( ![userBuilder userInfoUpToDate] ) { [userBuilder buildUser]; } STAssertTrue([userBuilder userInfoUpToDate], @"User information is not valid before building the user"); } 

What is the general practice for testing such things? It is expected that userInfoUpToDate will be updated asynchronously.

Thanks! William

+2
source share
2 answers

Sometimes it’s tempting to test things that you usually don’t test with Unit Testing. This mainly comes from a lack of understanding and a desire to experience everything. And then you realize that you don’t know how to test it with unit testing.

Better ask yourself - what am I testing here?

Am I checking that the data is not available until the request completes?

Then you can write a non-asynchronous version of the test that will verify that the data is available after the request is complete.

I verify that the response was saved correctly after the request?

You can also test it with flags in your logic.

You can run all logic tests without performing asynchronous tests.

So, I’ll even ask you why, in your opinion, you need to test an asynchronous call?

Unit testing should be quick β€” so consider this one more reason not to test asynchronous calls. Imagine a system of continuous integration that will conduct this test - it will take extra time.

And after reading your comments on another answer - I think it is not quite easy to use asynchronous testing. For instance. Kent Beck in the TDD Book. that simultaneous testing of a device is possible, but a very rare case.

So - what and why do you really want to check?

+4
source
+2
source

All Articles