Mock Testing

Davin Iddo
Moodah POS
Published in
2 min readNov 29, 2019

Inside an application, you may want to test an object that have a dependencies on other objects. Through the use of Mock testing, we could isolate the object by replacing the other object with mocks that simulate the behavior of the real object. By doing this we can pinpoint a mistake when a failed test happen.

Stubs

Stub is an object with a predetermined answer (data) that will be used to response to the call during the testing. We used stub when we don’t want to involve an object that would answer with any real data.

Below is our project implementation of the stub, we are testing the GraphQL resolver, so to replace the function that will call to the back-end Odoo we will replace it with a hard coded stub.

Stub test, created by Michael Sudirman from team Moodah

The code above shows the stub model/data to substitute or act as the database in the constant called posProduct, and the response that is expected to be found in the constant called queryAllPosProduct.

In the stub test, we created a service to do the query and at the end we will check if the response we get will be exactly as we expected or not.

Mock Testing

Mock test, while it is similar to stud, it has its own special method of doing the test. Mocks are object that register calls they receive, we verify for the behavior and the expected action all are performed correctly inside the test.

Below is our mock test implementation.

Mock test, created by Nicolaus from team Moodah

The code above shows that, we created the service at the start. Then we perform the query and check for its behavior, for example the first object we check we expect that the object has a “searchRead” operation and a “secure” client. We also used the jest library function fn(), this will allows us to use assertion function on these objects.

So we are essentially checking, if the createService is done in a correct way.

END

Mock testing can be really helpful on checking the behavior of an object and how it works correctly by itself (by assuming that all the dependencies are working fine), with this we can create a good, working object.

Bey

--

--

Davin Iddo
Moodah POS

A useless blob whose memory is worse than a goldfish