- Learn the difference between spies, mocks, and stubs
- Write tests for code with dependencies
In this exercise, you are given some code that already has partial coverage.
Edit calculator.ts; remove the lines about istanbul ignore
on line 28 & 29 that look like this:
/* istanbul ignore next */
By removing these lines, the class defined below it will begin to count against the code coverage reports. You should notice
your code coverage is no longer at 100%. Make sure your watcher is running (grunt watch
)
- Implement the remaining tests and fix any bugs you find in the process.
- Add
NegateExpression
(-n
) - inverts the providedExpression
. For example,NegateExpression(NumberExpression(-3))
would evaluate to 3. - Add
PowerExpression
(2^n
) - takes the givenExpression
and sets it as the exponent of 2. For example,PowerExpression(NumberExpression(3))
would evaluate to 8 - Make sure your code coverage is constantly at 100%.
In most of your previous tests, you probably used the NumberExpression
class. This might make
sense because in this case, NumberExpression
is very straight forward. The problem with this approach
is that if there is a bug in NumberExpression
, all of your subsequent tests break.
Replace the contents of NumberExpression
.eval()
so it always returns a random number between 1 - 3:
Math.ceil(Math.random() * 3)
Notice how most of your tests are now broken, and inconsistently at that! Fix all your tests so that each
passes regardless of what Expression
implementation is passed into it. Try approaches varying
between mocks, stubs, and dummy implementations of Expression
. When done, revert the "bug" introduced to
NumberExpression
.eval()
.
In this last example, we will examine using external dependencies. In this specific case, we will treat
JavaScript standard library Math
as one: you can't modify it, didn't write it, but need its behavior.
Add the following new Expression
type and write tests for it:
RandomExpression
(random(n)
) - return a random integer between 0 andExpression
, inclusively. For example,RandomExpression(NumberExpression(3))
would evaluate to a random number 0 - 3
Note that this last Expression
implementation's tests will absolutely require that you stub the Math.random
method.