mercredi 14 septembre 2016

Sinon js - stub property of a class

I am trying to stub a property from a js class using Sinon js. I have this service

class VersionService extends Service {
constructor({ endpointResolver, config, getToken }) {
    super({ getToken });
    this.fileCabinetApi = new RestService({ endpointResolver, config, getToken });
}

getVersion() {
    return this.fileCabinetApi.sendRequest({ requestType: 'GET', endpoint: '/api/version' });
}

}

I am trying to stub a function from fileCabinetApi but whatever I use I am receiveing the error

Attempted to wrap sendRequest which is already wrapped

I have this failing test

describe('VersionService', () => {
const config = {
    endpoint: 'fake',
};

let sandbox;
beforeEach(() => {
    sandbox = sinon.sandbox.create();
});

afterEach(() => {
    sandbox.restore();
});

const VersionServiceInstance = new VersionService({ config, getToken: () => Q.resolve('') });

it('getVersion => successfully returns resolved promise', () => {
    sandbox.stub(RestService.prototype, 'sendRequest', stubCall({ requestType: 'GET', returnValue: Version, callShouldFail: false }));
    let responseData = {};
    VersionServiceInstance.getVersion()
        .then(response => {
            responseData = response.data.body;
            expect(responseData).to.equal(Version);
        })
        .fin(() => {
            expect(responseData).to.not.be.equal({});
        });
});

});

Any ideas how to fix the error and make the test successful?

Aucun commentaire:

Enregistrer un commentaire