lundi 29 octobre 2018

Sinon stub method calling from another file

I'm trying to set up unit tests in my project. For that, I use mocha chai and sinon librairies. I'm using a NodeJs server version 8.

I'd like to stub a method which is declared in another file.

Here's an example :

- a.js
   exports.FnA(return FnB())
- b.js
   exports.FnB()

I want to stub method FnB() from b.js file so I can test FnA() regardless FnB() return.

Here's what I've tried :

beforeEach(() => {
            this.FnBStub = sinon.stub(b, 'FnB').returns('mocked');
        });

afterEach(() => this.FnBStub.restore());

it('should mocked FnB function', () => {
            try {
                console.log(b.FnB()); //returns 'mocked'
                console.log(a.FnA()); //return error from FnB() execution ...
            } catch (e) {
                console.log(e);
            }
        });

It does stub the method FnB() but only when I called it from an instance of b file. So when I called FnA() the stub seems to go away ...

What am I missing ?

Some help would be really appreciated, thanks :)

Aucun commentaire:

Enregistrer un commentaire