schmock
A mock object library for Node.js
0.0.5
Stable Release:Planned features:
- Argument Matchers
Quick Examples:
// complete (quiet) mocking with params and returnvar foo = schmock;foo;foo; assert; // PASSassert; // FAILassert; // PASSassert; // FAILassert; // FAILassert; // FAILassert; // FAILassert; // PASS // complete (loud) mocking with params and returnfoo = schmock;foo; assert; // OKfoo; // Error: expected argument:'1' did not match actual:'2' for argument 1foo; // Error: expected number of arguments:1 did not match actual:0foo; // Error: expected number of arguments:1 did not match actual:2 // complete mocking with throwfoo = schmock;foo;foo; // Error: Ham Sandwiches! // complete mocking with a callbackvar foo = schmock;foo;foo; // complete mocking with a callback and returnfoo = schmock;var callbackInvoked = false;foo;assert; // OK // complete mocking with a callback and throwfoo = schmock;callbackInvoked = false;foo;foo; // Error: OMG! // mocking without params and with a returnfoo = schmock;foo;assert; // OK // mocking without params, return and throwfoo = schmock;foo;foo; // Error: OMG! // verify method was called 'n' timesvar foo = schmock;foo;foo;foo; // OKfoo;foo; // "Error: mocked method:'bar' was called with [1,'2'] 2 times, but was expected 1 times" // expect a method to be called 'n' times (with no return or callback behavior)var foo = schmock;foo;foo;foo;foo; // OKfoo;foo; // "Error: mocked method:'bar' was called with [1,'2] 1 time, but was expected 0 times"
Usage
var schmock = ; // newly created mocks will throw errors when method parameters do not matchschmock; // newly created mocks will log errors and return null from methods when method parameters do not match// Defaultschmock; // create a mockvar foo = schmock;// create a mock with an optional name parameter for identification in console log messages (quiet mode)foo = schmock; // this mock will throw errors when method parameters do not matchfoo = foo; // this mock will log errors and return null from methods when method parameters do not matchfoo = foo; // mock the method 'bar'var bar = foo; // expect the params (1,'param2') to be passed to 'bar' when calledbar = bar; // return 'response' when 'bar' is calledbar; // return the result of function when 'bar' is calledbar; // throw an Error when 'bar' is calledbar; // invoke a callback when 'bar' is calledbar; // invoke a callback with params when 'bar' is calledbar; // invoke a callback with params and return a value when 'bar' is calledbar;// ORbar; // invoke a callback with params and throw an Error when 'bar' is calledbar;// ORbar;