Test Doubles ~ How do jasmine
/jest
/mocha
/chai
and sinon
stack-up
In the real world, jargon is sometimes confusing to the point some words may lose their intended meaning to some audiences. This blog re-injects clarity on some misconceptions around testing jargon.
In this article we will talk about:
- Confusing technical terms around testing tools
- Confusing technical terms around testing strategies
- How to easily remember “which is what” around testing tools
- How to easily remember “which is what” around test strategies
Even though this blog post was designed to offer complementary materials to those who bought my Testing
nodejs
Applications book, the content can help any software developer to tuneup working environment. You use this link to buy the book.
Show me the code
//order/model.js
Order.find()
.populate()
.sort()
.exec(function(err, order){
/** ... */
});
//order/middleware.js
new Order(params).save((error, order, next) => {
if(error) return next(error, null);
new OrderService(order).scheduleShipping();
return next(null, order);
});
Example: Used to showcase test double usage
Asking the right questions
When trying to figure out the “what does what” with testing stacks, the following questions gives a clue on how to differentiate one concept from the next and one tool from the next:
This article is unfinished business and will be adding more content as I gather more confusing terms, or find some more interesting use cases.
- What role does the test runner play when testing the code sample above
- What role does the test doubles play when testing the code sample above
- How do a test runner and test doubles stack up in this particular use case.
- How do Stubbing differ from Mocking
- How to Stubbing differs from Spy fakes ~ (functions with pre-programmed behavior that replace real behaviors)
- How can we tell the function
next(error, obj)
has been called with —order
orerror
arguments in our case? - What are other common technical terms that cause confusion around test strategies — Things such as unit test, integration test and end to end tests. Other less common, but confusing anyways, terms like smoke testing, exploratory testing, regression testing, performance testing, benchmarking, and the list goes on. We have to make an inventory of these terms(alone or in a team, and formulate a definition around each one before we adopt using them vernacular day-to-day communication).
- What are other common technical terms that cause confusion around testing tools — which tools are more likely to cause confusion, and how do they stack up in our test cases (test double tools: mocking tools, spying tools, stubbing tools versus frameworks such as
mocha
chai
sinon
jest
jasmine
etc) - How to easily remember which is what terms around test strategies such as unit/regression/integration/smoke/etc tests
- How to easily remember which is what terms around testing tools such as mocks/stubs/fakes/spies/etc tools.
In the test double universe, there is a clear distinction between a stub, a fake, a mock, and a spy. The stub is a drop-in replacement of a function or method we are not willing to run when testing a code block. A Mock, on the other hand, is a drop-in replacement of an object, most of the time used when data encapsulated in an object is expensive to get. The object can either be just data, or instance of a class, or both. A spy tells us when a function has been called, without necessarily replacing a function. A fake and a stub are pretty close relatives, and both replace function implementations.
Conclusion
In this blog, we explored differentiations around test strategies and testing tools. Without prescribing a solution, we let our thoughts play with possibilities around solutions and strategies that we can adapt for various use cases and projects.