gpt4 book ai didi

node.js - 无法在 sinon 测试用例中传递拒绝内的参数

转载 作者:太空宇宙 更新时间:2023-11-04 00:15:00 24 4
gpt4 key购买 nike

这是我尝试使用 sinon、chai、mocha 进行测试的一段 node.js 代码。然而我不明白为什么我无法在 sinon 的拒绝中传递参数。我尝试寻找在线帮助和文档,但仍然无法找到适当的原因。这是我正在尝试测试的代码:

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 

事实上,我在 equal 中给出的内容并不重要。请帮助解释为什么我无法传递参数来拒绝并期望它等于相同的参数。

最佳答案

希望我的回答对您有用。查看您的测试脚本.. 首先,您对 Repository.findSomething 进行 stub 以返回带有字符串“failed”的被拒绝的 promise 。因此,在实际的this.retrieveSomething代码中,会落入catch语句中:

.catch(function (err) {
reject(new errors.InternalServerError('Failed to find Surveys',
{errors: [{message: 'Failed '}, {details: err.errors}]}));
});

这意味着, (err) 将包含字符串“failed”,作为您 stub 的拒绝 promise 的结果。之后,this.retrieveSomething 将返回 Promise Reject,但它的值将由函数 error.InternalServerError 处理,该函数采用上面的 2 个参数。因此,函数 this.retrieveSomething 的拒绝值取决于您的 error.InternalServerError 实现。

还有一件事要注意,我认为你被拒绝的 stub findSomethingStub.returnsPromise().rejects("failed"); 不会产生任何效果,因为 error.InternalServerError 会抓取 err.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实现结果评估错误。在这种情况下,我们需要确保 details: 'failed' 与 stub 拒绝的内容匹配。如果我们用其他内容更改 details 值,测试将会失败。

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

关于node.js - 无法在 sinon 测试用例中传递拒绝内的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47573532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com