Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callsFake #768

Closed
wants to merge 2 commits into from
Closed

Add callsFake #768

wants to merge 2 commits into from

Conversation

edeustace
Copy link

This adds callsFake function similar to the jasmine's callFake

The benefits of such a function is that you can add a mock function inline with the parameters of the function declared. I find this easier to read than callsArgWith as this requires you to think about the function you want to mock in a different way. I find it more natural and readable sometimes to drop in a replacement function.

example:

    var foo = {
       bar: function(a, b, c){
           // do stuff..
           return 'baz';
        },
        barWithCallback: function(a, callback){
          callback(null, 'ok');
        }
    }

    foo.bar = sinon.stub.create().callsFake(function(a, b, c){
      return 'baz';  
    });

    //more readable than callsArgWith(1, null, 'ok');?
    foo.barWithCallback = sinon.stub.create().callsFake(function(a, callback){
      callback(null, 'ok');
    });

@mantoni
Copy link
Member

mantoni commented Jun 22, 2015

Can you explain what the difference is to these two use cases:

foo.barWithCallback = sinon.stub().yields(null, 'ok');

or

foo.barWithCallback = sinon.spy(function (a, callback) {
  callback(null, 'ok');
});

Thanks.

@edeustace
Copy link
Author

Ah - my apologies - I didn't see the sinon.spy(function(){}) api when going through the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants