Mock Testing

Nicolaus Christian Gozali
Moodah POS
Published in
3 min readNov 21, 2019

a way to isolate unit tests

src: Fake (Object Lessons) by Kati Stevens book cover

An application comprises of many integrated components working together. While testing however, it is best to isolate a unit that will be tested by assuming that every component it interacts with functions properly. This makes it easy to pinpoint the component at fault upon a failed test.

The practice of replacing real implementation with fakes is falsely known as mocking. A more general and correct term for it is Test Double that refers to replacement of production code for testing purposes.

That is then classified into 5 types: dummy, fake, stubs, spies and mocks. Specifically mock and stubs will be discussed here along with examples using Jest, a javscript testing framework, on my group project, Moodah POS which is a point of sale application with GraphQL Apollo server as its middle ware and Odoo, an opensource ERP system, as its back end.

Art of stubbing

Stub is an object that uses a predefined response to answer calls during testing. This technique is used if involvement of other components is not desired due to having to touch real data or cause side effects. When stubbing, state or return value of components are tested after some action is done. This is referred as state verification.

Referring to the code below, the createService function from Nodoo npm library is the one responsible to do fetching from Odoo. Since my goal is to unit test our GraphQL resolver, Odoo’s response will be stubbed with hard coded values.

stub test with Jest

Stubbed response should still have similar shape to its real implementation. However if that code’s underlying implementation is alien to you, then it is your job to do some researching. After looking around a bit myself, createService function returns a Stream object of either an error or success data that uses two other libraries: xstream and fp-ts. That is the purpose of xs.of(right(...))) , to match the shape expected by the next series of functions.

Note that at the end, the query result is checked against another predefined response after the query itself is executed for validating its state.

The Art of Mocking

Mocks are objects that register calls they receive. Behavior verification is done on Mocks, checking all expected actions were performed. This is used if executing production code is undesirable or it is hard to check some code is executed.

Looking below, implementation is replaced a similar one but with Jest fn() so that behavior assertion functions provided can be used. Response from createService is also stubbed here but now shown for brevity.

mock test with Jest

Assertions are made to verify number of function calls, arguments and return values. In this case it is done to verify that query does indeed call createService in correct way.

As interest in TDD grows, it is important to know several techniques to create unit tests. Hopefully, this will get you started into looking more into all sorts of test doubles and be able to use mock frameworks more effectively. That is all, cheers~

--

--