pretendr
Powerful JavaScript mocking
Install it
npm install pretendr
.
Use it
var pretendr = ;
Mock your objects. You can pass in real objects (pretendr(require("fs"))
) but
I prefer to keep mocks to a minimum so I know exactly what my code is doing.
var mockFs = ;
Each function creates a mock function and each object creates a mock object. (As
a shortcut, pretendr()
creates a standalone mock function which you can use as
a dummy callback.)
mockFs
now contains a mock
property, which is what you pass in to your code
for testing as a substitute for the real thing. This is virtually
indistinguishable to your code from the object you are mocking.
var fs = mockFsmock;fs;fs;
It works well with injectr, which allows you to pass in your mocks when testing.
var myLib = ;
Or you can use whichever dependency injection method you're used to.
Now let's monitor the calls:
assert;assert;
And run the callback, then test that it did what we expect:
fsreadFilecalls0;assert;
We can set return values:
mockFsreadFileSync;// ormockFsreadFileSync;
Templates allow you to create a new pretendr object each time the function is run:
mockFscreateReadStream;
Then retrieve your created pretendr:
var mockRs = mockFscreateReadStreamcalls0pretendr;assert;
If you have lots of function calls and you only want to test one of them, use
findCall
to find a call by it's arguments:
mockFs;
findCall
can also take a number for a number of arguments, or a function which
should return true for each matching argument.
Share it
pretendr is under the MIT License. Fork it. Modify it. Pass it around.