Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Sinon js-类的存根属性_Javascript_Unit Testing_Sinon - Fatal编程技术网

Javascript Sinon js-类的存根属性

Javascript Sinon js-类的存根属性,javascript,unit-testing,sinon,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()

我正在尝试使用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({});
        });
});
}))


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

这是唯一一个存根
发送请求的测试吗?此错误可能是由于以前的测试未将
sendRequest
还原为其原始版本而触发的。是的,这是一个问题。现在我不知道为什么在我需要在VersionServiceInstance.getVersion()时调用stubbed方法时,在sandbox.stub(RestService.prototype…)行之后立即调用stubbed方法调用。可能是因为
stubCall
做了什么?这是唯一一个stubs
sendRequest
的测试吗?错误可能是因为以前的测试没有将
sendRequest
还原到其原始状态而触发的。是的,这是一个问题。现在我不知道为什么在行sandbox.stub(RestService.prototype…当我需要在调用VersionServiceInstance.getVersion()时调用它时。可能是因为
stubCall
做了什么?