Sinon js - 类的存根属性

时间:2016-09-14 08:36:34

标签: javascript unit-testing sinon

我正在尝试使用Sinon js从js类中存根属性。我有这项服务

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' });
}

}

我正在尝试从fileCabinetApi存根函数,但无论我使用什么,我都会收到错误

  

尝试包装已经包装的sendRequest

我有这个失败的测试

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({});
        });
});

});

如何修复错误并使测试成功?

0 个答案:

没有答案