jupiter / simple-mock

Super simple stubs and spies with 1-step sandbox restore
MIT License
87 stars 12 forks source link

Mock function parameters only #29

Closed Cooya closed 5 years ago

Cooya commented 5 years ago

Hey, I am looking for a way to accomplish this with simple-mock :

const obj = {
    fct: str => console.log(str)
};

obj.fct('hello');
// display "hello"

obj.fct2 = obj.fct;
obj.fct = obj.fct2.bind(obj, 'hi');

obj.fct('hello');
// display "hi"

I just want to make a mock to replace only the parameters but keep the call to the original function.

jupiter commented 5 years ago

This should do it:

const simple = require('simple-mock')
simple.mock(obj, 'fct', obj.fct.bind(obj, 'hi'))
obj.fct('hello')
// display "hi"