Unit testing with iOS using XCTest and OCMock frameworks

nishant

Xcode unit tests are captured inside an XCTestCase subclass. XCTestCase is a concrete subclass of XCTest (abstract base class for testing),  that needs to be overridden for defining any custom tests.

Screen Shot 2017-04-03 at 2.28.39 PM.png

By norm, each XCTestCase subclass encapsulates multiple test methods with optional setup and teardown before and after tests are run.

Screen Shot 2017-04-03 at 12.46.46 PM.png

To write your own test cases, all you need to do is follow the below steps:

  1. Create a subclass of XCTestCase.
  2. Add one or more test method to the subclass.

Screen Shot 2017-04-03 at 12.56.09 PM.png

Test methods are instance methods that have no parameters, do not return a value, and whose name begins with the lowercase word “test”. To test the functionality of our method we could follow a certain pattern defined by the following:

  1. Given
  2. When
  3. Then

Screen Shot 2017-04-03 at 1.04.42 PM.png

For e.g. let’s say, we have got a class called MyClass with an initialization method called initWithDictionary. This method takes a dictionary parameter and assigns to its properties.

Screen Shot 2017-04-03 at 2.41.15 PM.png

In order to test the same, we are going to follow the below.

Screen Shot 2017-04-03 at 2.46.52 PM.png

Here, an assertion is to determine whether the test case is passed or failed. We could see that we happen to cover the positive case for the method. Now let’s try to cover the negative one as well.

Screen Shot 2017-04-03 at 2.51.02 PM.png

Also for code coverage of a method, we ideally should cover all the cases possible to test the methods. Like we could write one more negative case for the same but this time by passing a valid but an empty dictionary.

Screen Shot 2017-04-03 at 2.54.41 PM.png

WHY MOCKING ??

Till now what we have covered involves the classes and methods written by us. But in real time projects, there would be a lot of third party frameworks integrated inside our application. How do you test now?

  1. In order to test our own methods which involve calls to third party API’s, we would certainly have to use mocking because we are not bothered to test what is there inside external api’s.
  2. One such framework is OCMock which would help in mocking anything to everything related to third party api’s.
  3. Include the third party OCMock Framework –  #import <OCMock/OCMock.h>
    Screen Shot 2017-04-03 at 4.18.47 PM.png

For e.g. let say, we have a method in our class which makes a call to a third party api.

Screen Shot 2017-04-03 at 3.48.52 PM.png

In order to test the above functionality, we need to stub the third party api using the OCMock framework and call in a way that whatever we want from the api could be seen and tested for all the use cases. Below is the code to test the positive case.

Screen Shot 2017-04-03 at 4.17.10 PM.png

Negative code coverage for the same is depicted below:

Screen Shot 2017-04-03 at 4.21.57 PM.png

In regular programming, we often work on blocks and that is something tricky when it comes to writing unit test cases for the same. So here is an example which would help us write unit tests seamlessly.

Let say we have a class called Util which has got two functions with block parameter.

Screen Shot 2017-04-11 at 4.14.43 PM.png

And we have a place in myMethod, where we have utilized the Util method.

Screen Shot 2017-04-11 at 4.21.01 PM.png

Now in order to test the method, myMethod, we first need to mock the Util dispatch methods.  Only then we would be able to write proper unit test cases accordingly.

Screen Shot 2017-04-11 at 4.25.42 PM.png

To learn more on OCMock, please refer to OCMock website – Front Page · OCMock.

Google Certified Agency

about the author

nishant