【问题标题】:How to unit test promise with sinon in my case?在我的情况下,如何使用 sinon 对 promise 进行单元测试?
【发布时间】:2017-03-27 08:15:33
【问题描述】:

我正在尝试为我的应用程序在单元测试中启动存根。

我的文件中有这样的内容。

var sinon = require('sinon'),
    should = require('should');

require('sinon-stub-promise');

describe('test unit Tests', function(){
    describe('My first test', function(){
        it('should test a promise', function(){
            sinon.stub().resolves('foo')().then(function (value) {
            console.log('test');

            assert.equal(value, 'not foooo')
        })
    })
});

我的问题是我似乎无法触发assert.equal error。运行测试时,我可以在输出中看到“test”。但是,测试应该失败,因为值应该是foo 而不是'not foooo'。由于某种原因,它通过了。我不确定原因。有人可以帮我吗?非常感谢!

【问题讨论】:

    标签: node.js unit-testing sinon


    【解决方案1】:

    您需要返回承诺,以便 mocha 等待:

    describe('test unit Tests', function(){
        describe('My first test', function(){
            it('should test a promise', function(){
                return sinon.stub().resolves('foo')().then(function (value) {
                  console.log('test');
    
                  assert.equal(value, 'not foooo')
                })
            })
        })
    });
    

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 2016-12-21
      • 2016-05-26
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多