0.4.0 • Published 9 years ago

expect-called v0.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

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 objectfunctionName, 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

optstypeuse for
withThisbooleanregister this object in calls
returnsArraycreate a mock function that returns each of the elements in the list one by one
mocksArray of functionfuncions 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

0.4.0

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago