expect-called

0.4.0 • Public • Published

expect-called

NPM Version NPM Downloads Linux Build Windows Build Test Coverage

If you want to verify that a function was called in the way you expect.

Install

$ npm install expect-called

API

expectedCalled.control(object, functionName, [opts])

Creates a function wrapper for object[functionName], the wrapper function will register de parameters used to call the function (included this) and then calls de original function and returns the returned value.

Returns a control object

opts type use for
withThis boolean register this object in calls
returns Array create a mock function that returns each of the elements in the list one by one
mocks Array of function funcions to call instead original function. Each element of the array is called in each call with the same parámeters and returning the corresponding returned value

control.call

Is the array property that contains each call to the function. Each call generates a object with two properties {This: ..., args: [... ]}

control.stopControl()

Stops the control function.

expectedCalled.global

SYMBOL that represents the root object in control.call

Example

var expect = require('expect.js'); // needed for the example
 
var expectCalled = require('expect-called');
 
var myModule={
    upper:function(word){
        return word.substring(0,1).toUpperCase()+word.substring(1);
    },
    cammel:function(phrase){
        // function to TEST and to CONTROL
        // must call upper
    }
}
 
describe('this test',function(){
  it('should call intermediate function',function(){
    var control = expectCalled.control(myModule,'upper');
    var phrase = 'this is my camel';
    var camelPhrase = myModule.cammel(phrase);
    expect(camelPhrase).to.eql('ThisIsMyCamel');
    expect(control.calls).to.eql([
        ['this'],
        ['is'],
        ['my'],
        ['camel']
    ]);
    control.stopControl();
  });
});

Notes

  • Not usefull for local functions that do not belong to a object
  • Not control de returned value but could specify the returned values (like a mock function)
  • Call to control with the same function name twice without stopping the previous control could generate unexpected results.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i expect-called

Weekly Downloads

59

Version

0.4.0

License

MIT

Last publish

Collaborators

  • tute