在sinon测试用例中无法在reject内传递参数

问题描述 投票:1回答:1

这是我尝试使用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 

事实上,我内心平等无所谓。请帮助我为什么我无法传递参数拒绝并期望它等于相同的论点。

node.js mocha sinon chai
1个回答
1
投票

我希望我的答案在这里很有用。查看您的测试脚本..首先,您使用字符串'failed'来返回被拒绝的承诺Repository.findSomething。因此,在实际的this.retrieveSomething代码中,它将落入catch语句:

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

这意味着,(错误)将包含字符串'failed'作为拒绝承诺的结果。在那之后,this.retrieveSomething将返回promise拒绝,但它的值将由函数error.InternalServerError处理,该函数带有如上所述的2个参数。因此,函数this.retrieveSomething的被拒绝的值取决于你对error.InternalServerError的实现。

另外需要注意的是,我认为你被拒绝的存根qazxsw poi不会产生任何影响,因为error.InternalServerError会抓住qazxsw poi所以至少它应该是这样的:findSomethingStub.returnsPromise().rejects("failed");

我试图通过假设error.InternalServerError返回如下字符串来模拟你的代码:

err.errors

然后,在测试用例中我试试

findSomethingStub.returnsPromise().rejects({errors: "failed"});

首先,它将断言promise.should.be.rejected,然后根据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); }); }); 值,测试将失败。

details: 'failed'
© www.soinside.com 2019 - 2024. All rights reserved.