Node.js 无法在sinon测试用例中的reject内传递参数
下面是我试图使用sinon、chai、mocha测试的node.js代码片段,。 然而,我不明白为什么我无法在sinon的拒绝中传递参数。我试图寻找在线帮助和文档,但仍然找不到合适的理由。以下是我尝试测试的代码:Node.js 无法在sinon测试用例中的reject内传递参数,node.js,mocha.js,sinon,chai,Node.js,Mocha.js,Sinon,Chai,下面是我试图使用sinon、chai、mocha测试的node.js代码片段,。 然而,我不明白为什么我无法在sinon的拒绝中传递参数。我试图寻找在线帮助和文档,但仍然找不到合适的理由。以下是我尝试测试的代码: this.retrieveSomething = function () { var promiseFunc = function (resolve, reject) { Repository.findSomething( {$or: [{"status":"x"},{
this.retrieveSomething = function () {
var promiseFunc = function (resolve, reject) {
Repository.findSomething( {$or: [{"status":"x"},{"status":"y"}]}, 'name description status')
.then(function (result) {
resolve(result);
})
.catch(function (err) {
reject(new errors.InternalServerError('Failed to find Surveys',
{errors: [{message: 'Failed '}, {details: err.errors}]}));
});
};
return new Promise(promiseFunc);
};
这是测试代码
it('failure', function (done) {
var findSomethingStub = sinon.stub(Repository, 'findSomething');
findSomethingStub.returnsPromise().rejects();
var promise = fixture.retrieveSurveysVast();
setTimeout(function () {
expect(findSomethingStub.calledOnce).to.be.true;
expect(promise).to.be.eventually.deep.equal("failed");
Repository.findSomething.restore();
done();
}, 5);
});
这个案子成功地通过了。然而,如果我试图以这种方式拒绝它,这会显得很奇怪
findSomethingStub.returnsPromise().rejects("failed");
像这样搭配
expect(promise).to.be.eventually.deep.equal("failed");
上面说
Unhandled rejection InternalServerError: Failed to find Surveys
事实上,我给你什么并不重要。请帮助解释为什么我不能传递参数来拒绝,并期望它等于相同的参数。我希望我在这里的回答会有用。 查看您的测试脚本。。首先,您将
Repository.findSomething
存根化,以返回字符串“failed”的拒绝承诺。因此,在实际的this.retrieveSomething
代码中,它将属于catch语句:
.catch(function (err) {
reject(new errors.InternalServerError('Failed to find Surveys',
{errors: [{message: 'Failed '}, {details: err.errors}]}));
});
这意味着,(err)将包含字符串“failed”,因为您的承诺被拒绝。之后,this.retrieveSomething
将返回promise-reject,但它的值将由函数error.InternalServerError
处理,该函数采用上述两个参数。因此,函数this.retrieveSomething
的拒绝值取决于error.InternalServerError
的实现
另外需要注意的是,我认为被拒绝的存根找到了somethingstub.returnsPromise().rejects(“失败”)
不会起任何作用,因为error.InternalServerError会抓取errr.errors
,所以至少应该是这样:findSomethingStub.returnsPromise().rejects({errors:“failed”})代码>
我试图通过假设该错误来模拟您的代码。InternalServerError返回如下字符串:
class Errors {
InternalServerError(string, obj) {
return `error: ${string}, message: ${obj.errors[0].message}, details: ${obj.errors[1].details}`;
} }
然后,在测试用例中,我尝试
const fixture = require('../47573532-sinon-react/Fixture');
const Repository = require('../47573532-sinon-react/Repository');
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const sinonStubPromise = require('sinon-stub-promise');
sinonStubPromise(sinon);
chai.use(chaiAsPromised);
chai.use(sinonChai);
chai.should();
describe('/47573532-sinon-react', () => {
it('failure (using sinon, chai, sinon-stub-promise, chai-as-promised)', (done) => {
const findSomethingStub = sinon.stub(Repository, 'findSomething');
findSomethingStub.returnsPromise().rejects({ errors: 'failed' });
const promise = fixture.retrieveSurveysVast();
promise.should.be.rejected.then((err) => {
err.should.be.deep.equal
(
'error: Failed to find Surveys, message: Failed , details: failed'
);
findSomethingStub.restore();
}).should.notify(done);
});
});
首先,它将断言promise.should.be.rejected,然后根据InternalServerError实现结果评估err。在这种情况下,我们需要确保details:'failed'
与存根拒绝的内容相匹配。如果我们用其他方法更改详细信息
值,测试将失败
it('failure (using sinon, chai, sinon-stub-promise, chai-as-promised)', (done) => {
const findSomethingStub = sinon.stub(Repository, 'findSomething');
findSomethingStub.returnsPromise().rejects({ errors: 'failed' });
const promise = fixture.retrieveSurveysVast();
promise.should.be.rejected.then((err) => {
err.should.be.deep.equal
(
'error: Failed to find Surveys, message: Failed , details: something not from stub'
); // this will cause test failed
findSomethingStub.restore();
}).should.notify(done);
});