解析promise的Sinon.JS存根返回“{}”

问题描述 投票:0回答:2

我有一个返回promise的Node.js函数。我正在使用Sinon.JS stubs来解决这个承诺。代码中的我的console.log语句显示存根正在运行。但是,返回的是{}而不是Promise解决的内容。

我回顾了这些其他SO帖子,但两者都不是我遇到的问题:

这是功能:

function publishMessage(pubsub, topicName, data) {   
  const topic = pubsub.topic(topicName);   
  const publisher = topic.publisher();

  return publisher.publish(data)
    .then((results) => {
      const messageId = results[0];
      return messageId;
    })
    .catch((error) => {
      console.log('Error ', error);
      return error;
    }); };

这是测试:

describe('publishMessage', function() {
  describe('Success', function() {
    it('should return the messageId', function(done) {
      var publishMessage = index.__get__('publishMessage');
      var promise = sinon.stub().resolves(['1111']);
      var publisher = {
        publish: promise
      };
      var topic = {
        publisher: sinon.stub().returns(publisher)
      };
      var pubsub = {
        topic: sinon.stub().returns(topic)
      };
      assert.equal('1111', publishMessage(pubsub, 'st', 'ds'));
      assert.isTrue(topic.publisher.calledWith());
      done();
    });
  });
});

当我执行测试时,console.log的输出显示打印出的分辨率值:

  publishMessage
    Success
      1) should return the messageId
1111


  0 passing (256ms)
  1 failing

  1) publishMessage
       Success
         should return the messageId:
     AssertionError: expected '1111' to equal {}
      at Context.<anonymous> (test/index.spec.js:63:14)
node.js sinon
2个回答
2
投票

我注意到有一些潜在的问题领域。

首先,我没有看到index的定义,所以我无法确认你期望的函数是否从index.__get__('publishMessage');返回。您可以通过目视检查结果来确认返回正确的函数

publishMessage.toString();

我看到的另一个问题(更可能是你的问题的原因)是你从publishMessage()返回一个Promise,但是将对该函数的调用结果与Promise最终解析的值进行比较。换句话说,您将Promise与String进行比较。除非你的断言库在检查结果之前等待Promise解析(类似于Jasmine),否则你将String与Promise进行比较。要解决这个问题,只需等待Promise解决:

it('should return the messageId', function(done) {
    // Set up the test case by defining publishMessage, etc.

    publishMessage(pubsub, 'st', 'ds').then((result) => {
        assert.equal(result, '1111');
        assert.isTrue(topic.publisher.calledWith());

        done();
    }).catch(done);
}

注意我在Promise上添加了一个.catch()。这可以确保Promise中抛出的任何错误都会显示相应的错误,而不仅仅是一个超时错误。

如果你正在使用像Mocha或Karma / Jasmine这样的测试框架,你可以通过直接返回Promise而不是使用done()来改善这一点。根据我的经验,在尝试调试使用Promise的测试用例时,返回Promise会产生更好的堆栈跟踪以及更有用和准确的错误消息。举个例子:

it('should return the messageId', function() {
    // Set up the test case by defining publishMessage, etc.

    return publishMessage(pubsub, 'st', 'ds').then((result) => {
        assert.equal(result, '1111');
        assert.isTrue(topic.publisher.calledWith());
    });
}

请注意,我不再接受测试用例中的参数。在Mocha和Karma中,这就是框架如何确定如何处理测试用例。


2
投票

你不要等待你的承诺得到解决。

尝试

publishMessage(pubsub, 'st', 'ds').then(result => {
  assert.equal('1111', result);
  assert.isTrue(topic.publisher.calledWith());
  done();
}
© www.soinside.com 2019 - 2024. All rights reserved.