schmock

0.0.5 • Public • Published

schmock

A mock object library for Node.js

Stable Release: 0.0.5

Planned features:

  • Argument Matchers

Quick Examples:

// complete (quiet) mocking with params and return
var foo = schmock.mock('foo');
foo.when('bar').with(1,'2').return('response');
foo.when('bar').with(3,'4').return('response2');
 
assert.equal('response',foo.bar(1,'2'));            // PASS
assert.equal('response',foo.bar(1));                // FAIL
assert.equal(null,foo.bar(1));                      // PASS
assert.equal('response',foo.bar(1,'3'));            // FAIL
assert.equal('response',foo.bar(1,'2','3'));        // FAIL
assert.equal('response',foo.bar(1,undefined));      // FAIL
assert.equal('response',foo.bar(1,null));           // FAIL
assert.equal('response2',foo.bar(3,'4'));           // PASS
 
// complete (loud) mocking with params and return
foo = schmock.mock('foo').loud();
foo.when('bar').with(1).return('response');
 
assert.equal('response',foo.bar());     // OK
foo.bar(2);                             // Error: expected argument:'1' did not match actual:'2' for argument 1
foo.bar();                              // Error: expected number of arguments:1 did not match actual:0
foo.bar(1,2);                           // Error: expected number of arguments:1 did not match actual:2
 
// complete mocking with throw
foo = schmock.mock('foo');
foo.when('bar').with(1234).throw(new Error('Ham Sandwiches!'));
foo.bar(1234);  // Error: Ham Sandwiches!
 
// complete mocking with a callback
var foo = schmock.mock('foo');
foo.when('bar').callback(function (one,two,three) {
    console.log(one);   // Prints 1
    console.log(two);   // Prints 2
    console.log(three); // Prints 3
}).with(1,2,3);
foo.bar();
 
// complete mocking with a callback and return
foo = schmock.mock('foo');
var callbackInvoked = false;
foo.when('bar').return(1234).callback(function (one,two,three) {
    console.log(one);           // Prints 1
    console.log(two);           // Prints 2
    console.log(three);         // Prints 3
}).with(1,2,3);
assert.equal(1234,foo.bar());   // OK
 
// complete mocking with a callback and throw
foo = schmock.mock('foo');
callbackInvoked = false;
foo.when('bar').callback(function (one) {
   console.log(one);                    // Prints 1
   callbackInvoked = true;
}).with(1).throw(new Error('OMG!'));
foo.bar();                              // Error: OMG!
 
// mocking without params and with a return
foo = schmock.mock('foo');
foo.when('bar').return(1234);
assert.equal(1234, foo.bar());  // OK
 
// mocking without params, return and throw
foo = schmock.mock('foo');
foo.when('bar').throw(new Error('OMG!'));
foo.bar();  // Error: OMG!
 
// verify method was called 'n' times
var foo = schmock.mock('foo');
foo.when('bar').with(1,'2').times(1);
foo.bar(1,'2');
foo.verify();       // OK
foo.bar(1,'2');
foo.verify();       // "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.mock('foo');
foo.expect('bar').with(1,'3').times(1);
foo.expect('bar').with(1,'2').times(0);
foo.bar(1,'3');
foo.verify();       // OK
foo.bar(1,'2');
foo.verify();       // "Error: mocked method:'bar' was called with [1,'2] 1 time, but was expected 0 times"

Usage

var schmock = require('schmock');
 
// newly created mocks will throw errors when method parameters do not match
schmock.loud();
 
// newly created mocks will log errors and return null from methods when method parameters do not match
// Default
schmock.quiet();
 
// create a mock
var foo = schmock.mock();
// create a mock with an optional name parameter for identification in console log messages (quiet mode)
foo = schmock.mock('foo');
 
// this mock will throw errors when method parameters do not match
foo = foo.loud();
 
// this mock will log errors and return null from methods when method parameters do not match
foo = foo.quiet();
 
// mock the method 'bar'
var bar = foo.when('bar');
 
// expect the params (1,'param2') to be passed to 'bar' when called
bar = bar.with(1,'param2');
 
// return 'response' when 'bar' is called
bar.return('response');
 
// return the result of function when 'bar' is called
bar.return(function() {
    return 'response'; 
});
 
// throw an Error when 'bar' is called
bar.throw(new Error('oops!'));
 
// invoke a callback when 'bar' is called
bar.callback(function() {
   console.log('callback invoked');
});
 
// invoke a callback with params when 'bar' is called
bar.callback(function(one,two,three) {
}).with(1,2.3);
 
// invoke a callback with params and return a value when 'bar' is called
bar.callback(function(one,two,three) {
}).with(1,2.3).return('response');
// OR
bar.return('response').callback(function(one,two,three) {
}).with(1,2.3);
 
// invoke a callback with params and throw an Error when 'bar' is called
bar.callback(function(one,two,three) {
}).with(1,2.3).throw(new Error('oops!'));
// OR
bar.throw(new Error('oops!')).callback(function(one,two,three) {
}).with(1,2.3);

Readme

Keywords

none

Package Sidebar

Install

npm i schmock

Weekly Downloads

0

Version

0.0.5

License

MIT

Last publish

Collaborators

  • zdj