Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding function with Sinon.mock?

The document says

var expectation = mock.expects("method"); Overrides obj.method with a mock function and returns it. See expectations below.

What's the syntax for that?

I tried

var mock = sandbox.mock(myObj).expects(myObj, "myfunc", function(){
                console.log('please!!!')
            }).once();

and

    var mock = sandbox.mock(myObj).expects("myfunc", function(){
                console.log('please!!!')
            }).once();

But neither of them work.

like image 387
fuiiii Avatar asked Sep 27 '22 16:09

fuiiii


1 Answers

Nitpick: you named your variable mock, but expects() returns an expectation.

In any case, Sinon documentation says that mock() takes a single argument and returns a mock object. expects() returns an expectation, which is both a spy and a stub, so you could do something like this:

var mock = sinon.mock(myObj).expects('myfunc').returns('something');

If you wanted to replace myObj.myfunc with a custom function, you could use a stub, perhaps like this:

var stub = sinon.stub(myObj, 'myfunc', function() {
    console.log('something');
});
like image 58
psquared Avatar answered Oct 13 '22 15:10

psquared