mockmock
Mocking library for minimalists and people that don't want to remember 500 different methods.
Why?
I like sinon, but I am not a big fan of forcing users to remember the difference between stubs, spies and mocks. There is no need for a ton of different methods. mockmock
is a much simpler approach that offers a sufficient solution for the majority of use-cases.
Example
mockmock
exports a single function mock
.
mock
accepts the function to be mocked or a constant value to be returned:
var mocked = // #=> A red apple // #=> A yellow banana // #=> A green cucumber
var mocked = console // #=> 123
Apart from defining behavior, the mock function is also a "spy", which means you can access its arguments, thisArg etc.:
var mocked = console // #=> [[1, 2, 3]] console // #=> [[1, 2, 3], [4, 5, 6]] mockedconsole // #=> [this, this, {}]
Install
With npm do:
npm i mockmock -S
mockmock
can then be require
d as follows:
var mock =
or:
var mock = mock
API
If you are familiar with sinon, a lot of the methods might look familiar to you.
mock()
#=> mockedFn
Either accepts a constant value to be returned using an identity function or a function to be mocked. If no argument is provided, a noop function will be used.
var mockedIdentityFn = console // #=> '123' var mockedFn = console // #=> 'hello' var mockedNoop = console // #=> undefined
args
An array of recorded arguments
. arguments
will be converted to an actual array to allow easy usage of deepEqual
methods.
var mocked = console // #=> [[1, 2, 3], [4, 5, 6]]
thisValues
Similar to args
, records the thisValue
the mocked function has been called with.
var mocked = mockedmockedconsole // #=> [{ a: 'a' }, { b: 'b' }]
returnValues
Similar to args
, records the return values of the mocked function.
var i = 0var mocked = console // #=> [0, 1, 2]
calls
Keeps track of when (unix timestamp) the mocked function has been called.
var mocked = console // #=> [1445386361361, 1445386361365, 1445386361369]
errors
Records the errors thrown by the function that has been mocked. Note that thrown errors will be passed through the mock function.
var error = var mocked = try catch e {}console // #=> [error]
callCount
How many times has the mocked function been called?
var mocked = console // #=> 3
called
If the mocked function has been called at all.
var mocked = console // #=> falseconsole // #=> true
calledOnce
calledTwice
calledThrice
If the mocked function has been called exactly once, twice or three times.
var mocked = console // #=> falseconsole // #=> trueconsole // #=> falseconsole // #=> true
firstCall
secondCall
thirdCall
lastCall
Returns an object describing the first, second, third or last call.
var mocked = console // #=> { thisValue: this, args: [1, 2, 3], returnValue: 'hello world', error: undefined }
clear
, flush
, reset
Resets the internal spy. All recorded arguments, errors, return values etc. will be reset.
var mocked = console // #=> truemockedclearconsole // #=> false
mockFn
The function that is being mocked. Identity function of the passed in value when a constant value has been supplied.
var {}var mocked = console // #=> true
mockValue
Equivalent of mockFn
for mocked identities. References the supplied constant value (if any).
var c = 123var mocked = console // #=> true
For more usage examples, have a look at the test suite.
Credits
- "Curious Chicken" image by Ian Southwell, licensed under CC BY-NC-ND 2.0
License
ISC