代码之家  ›  专栏  ›  技术社区  ›  Julian

mocha/sinon存根还原不工作

  •  1
  • Julian  · 技术社区  · 7 年前

    我的第一个节点测试脚本。我正在使用Mocha和Sinon测试最终发送到队列的函数。节点v8.1.3、Mocha 5.0.2、sinon 4.4.2。

    var assert = require('assert');
    var sinon = require('sinon');
    var uut = require('../util');
    var stub;
    
    describe('processWantedItemToQueue', function() {
        beforeEach(function() {
          stub = sinon.stub(uut, 'processWantedItemToQueue').returns(true);
        });
    
        afterEach(function() {
          uut.processWantedItemToQueue.restore();
        });
    
        it('should not push the item to the queue', function() {
          assert.equal(uut.processWantedItemToValidQueue("a", "q1"), false);
        });
    
        it('should push the item to the queue', function() {
          assert.equal(uut.processWantedItemToValidQueue("a", "aws"), true);
        });
    });
    

    这是我的输出:

    > mocha
      processWantedItemToQueue
    ignoring queue q1
        ✓ should not push the item to the queue
        1) should push the item to the queue
    
    
      1 passing (34ms)
      1 failing
    
      1) processWantedItemToQueue
           should push the item to the queue:
         ReferenceError: processWantedItemToQueue is not defined
          at Object.processWantedItemToValidQueue (util.js:36:20)
          at Context.<anonymous> (test/test.js:20:24)
    

    我对存根有些误解。为什么我的第二次测试没有找到存根?

    我的util。js如下所示:

    module.exports = {
      processWantedItem: function(item) {
      ...
      },
      processWantedItemToQueue: function(item, queue) {
      ...
      },
      processWantedItemToValidQueue: function(item, queue) {
      ...
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   yoz    7 年前

    看起来这不是存根的问题,而是实现的问题 processWantedItemToValidQueue ; 很可能你只是想打电话 processWantedItemToQueue 在第二种情况下,但是 processWantedItemToQueue 正如您所定义的,它不在其他函数的范围内。当然,您必须展示 processWantedItemToValidQueue .