Unit Testing with RxSwift — JoyDev Blog — Sharing Experience

JOY DEV
3 min readOct 6, 2020

How to implement unit testing in the projects using reactive libraries, for example, RxSwift? Read here!

Testing plays an important role in the development of any software product. It makes it possible to detect bugs at various steps of development and get more qualitative results.

There are different testing types at the given moment. The testing of a complete product as important as unit testing. The advantage of this approach is the opportunity to check the performance of each unit. Moreover, it becomes easier to detect bugs and fix them at an early stage. It significantly reduces the cost of development. The cost of bugs detection at different stages varies dramatically. If a bug is detected in unit testing, the cost won’t be high. But if it is detected at the release stage, the expenses will rise several times.

In this article, we are going to view how to implement unit tests in the project using reactive libraries.

Application Architecture

Our app is built on MVVM architecture with the use of controllers. The app is divided into logical units, each of them consists of ViewModel, Model, ViewController, and coordinates. ViewModel implements the StateMachine pattern. The connection between ViewModel and ViewController component is realized with the help of the Reactor Kit library. To bind ViewModel and ViewController RxSwift is used.

App Problem

Since the reactive libraries are used in the app, it’s rather complicated to implement unit testing. The reactivity is hard to check with unit tests, as the code is asynchronous.

Solution

That’s why we decided to use the RxBlocking library to test ViewModel. It has two main advantages.

1. With the help of this library some parts of the code can be made subsequent. The function will wait to get some value from the variable observable, in this case from the state ViewModel. The library allows sorting the received signals. It’s possible to miss, add a delay, complete the test by time-out, if the signal wasn’t received during some period of time. Unit tests should be fulfilled very fast. That’s why we can fail the test by time-out so that we didn’t wait for it too long. For example, if the signal weren’t received for 5 seconds, it’s considered to be failed.

2. ViewModel uses a variety of services that address the databases and external APIs. These inquiries may take too much time that depends on many factors (for example, the Internet speed and availability, the number of accounts in the database). But one of the conditions of unit testing is that in similar conditions the tests should provide the same result. They shouldn’t depend on the conditions we can control. That’s why fake or mocked servers are often used in unit testing. They plug the data set defined in advance and fulfill tests there.

However, the disadvantage of this approach is the inability to detect bugs at other levels. As the name implies, unit testing can check the functionality only of separate code units. So other bugs in integration or other system bugs in performance remain undetected. So it’s better to use unit tests only with other testing types.

Summary

So, the use of unit tests saves time for bug detection and fixing. This type of testing has its own peculiarities for different types of programming, so it should be taken into account when writing the tests.

Have an idea of a cool project? Write to us and together we’ll create a high-quality app!

Originally published at https://joy-dev.com/unit-testing-with-rxswift/ on October 6, 2020.

--

--

JOY DEV

The team of ambitious web and mobile developers for your projects. From startups to enterprises and banking solutions! Follow https://joy-dev.com/