1.0.1 • Published 10 years ago

simplespy v1.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
10 years ago

SimpleSpy.js

npm.io Dependency Status

Standalone library for creating spies in Node.js. Easy to use. No dependancies.

npm install simplespy

Usage

SimpleSpy exports a simple spyOn function that accepts a single function as a parameter and returns a spy. SimpleSpy does not modify the given function.

spyOn

Accepts a function. Returns a spy.

var spyOn = require('simple-spy');

function add5(num) {
  return num + 5;
}

var spy = spyOn(add5); // create spy

callCount

Doesn't accept arguments. Returns the number of times the spy has been called.

var spyOn = require('simple-spy');

function add5(num) {
  return num + 5;
}

var spy = spyOn(add5); // create spy

spy(2); // returns 7
spy(3) // returns 8


spy.callCount(); // returns 2

wasCalled

Doesn't accept arguments. Returns a boolean indicating if the spy has been called.

var spyOn = require('simple-spy');

function add5(num) {
  return num + 5;
}

var spy = spyOn(add5); // create spy

spy(2); // returns 7
spy(3) // returns 8


spy.callCount(); // returns 2
spy.wasCalled(); // returns true

wasCalledWith

Accepts a single argument. Returns a boolean indicating if the spy has been called with the given argument.

var spyOn = require('simple-spy');

function add5(num) {
  return num + 5;
}

var spy = spyOn(add5); // create spy

spy(2); // returns 7
spy(3) // returns 8


spy.callCount(); // returns 2
spy.wasCalled(); // returns true
spy.wasCalledWith(2); // returns true

returned

Accepts a single argument. Returns a boolean indicating if the spy has been called with the given argument.

var spyOn = require('simple-spy');

function add5(num) {
  return num + 5;
}

var spy = spyOn(add5); // create spy

spy(2); // returns 7
spy(3) // returns 8


spy.callCount(); // returns 2
spy.wasCalled(); // returns true
spy.wasCalledWith(2); // returns true
spy.returned(7); // returns true

Running tests

npm test

License

MIT. Copyright (c) Sterling Whitley