Javascript Sinon spy on console.log调用未注册

Javascript Sinon spy on console.log调用未注册,javascript,testing,mocha.js,sinon,chai,Javascript,Testing,Mocha.js,Sinon,Chai,我正试图了解Sinon并想监视console.log。代码很简单: function logToConsole() { console.log('Hello World'); } exports.logToConsole = logToConsole; 但是如果我想测试它,它就不起作用了,因为对console.log的调用没有在被测系统中注册: var chai = require('chai'), expect = chai.expect, sinonChai =

我正试图了解Sinon并想监视
console.log
。代码很简单:

function logToConsole() {
    console.log('Hello World');
}

exports.logToConsole = logToConsole;
但是如果我想测试它,它就不起作用了,因为对
console.log
的调用没有在被测系统中注册:

var chai = require('chai'),
    expect = chai.expect,
    sinonChai = require('sinon-chai'),
    sinon = require('sinon'),
    sut = require('../src/logToConsole');

chai.use(sinonChai);

describe('logToConsole', function() {
    it('should spy on console.log', function() {
        sinon.spy(console, 'log');

        sut.logToConsole();

        expect(console.log).to.have.been.called;
    });
});
但是,如果我在测试本身内部执行
console.log
,它将被捕获并通过:

it('should spy on console.log', function() {
    sinon.spy(console, 'log');

    sut.logToConsole();
    console.log('Test');

    expect(console.log).to.have.been.called;
});
有趣的是,它似乎根本无法监视内部函数调用。这不是间谍图书馆的目的吗

e、 g


您似乎没有实际使用sinon chai,您发布的代码缺少以下行:

chai.use(sinonChai);
编辑:以下是我测试的代码:

// test.js
var chai = require('chai'),
    expect = chai.expect,
    sinonChai = require('sinon-chai'),
    sinon = require('sinon'),
    sut = require('./log');

chai.use(sinonChai);

describe('logging', function() {

  beforeEach(function() {
    sinon.spy(console, 'log');
  });

  afterEach(function() {
    console.log.restore();
  });

  describe('logToConsole', function() {
    it('should log to console', function() {
      sut.logToConsole();
      expect(console.log).to.be.called;
    });
  });

  describe('logToConsole2', function() {
    it('should not log to console', function() {
      sut.logToConsole2();
      expect(console.log).to.not.be.called;
    });
  });
});

// log.js
module.exports.logToConsole = function() {
  console.log('Hello World');
};

module.exports.logToConsole2 = function() {
};

对不起,我忘了在我的问题中写下它,但它不会改变任何事情。这会将“Hello World”输出到测试控制台-如何隐藏它?@maasha恐怕不容易。问题是摩卡本身也使用
console.log()
进行输出,因此如果禁用
console.log()
(例如,通过对其进行存根而不是监视),您也将丢失(部分)摩卡的测试输出。这是不直接使用
console
的另一个原因。相反,请使用记录器库或函数,这样您就可以将其存根。@StijndeWitt,如果您正在为浏览器和节点创建同构的日志库,该怎么办。。。你不能把问题推到一边去,那是不好的做法。
// test.js
var chai = require('chai'),
    expect = chai.expect,
    sinonChai = require('sinon-chai'),
    sinon = require('sinon'),
    sut = require('./log');

chai.use(sinonChai);

describe('logging', function() {

  beforeEach(function() {
    sinon.spy(console, 'log');
  });

  afterEach(function() {
    console.log.restore();
  });

  describe('logToConsole', function() {
    it('should log to console', function() {
      sut.logToConsole();
      expect(console.log).to.be.called;
    });
  });

  describe('logToConsole2', function() {
    it('should not log to console', function() {
      sut.logToConsole2();
      expect(console.log).to.not.be.called;
    });
  });
});

// log.js
module.exports.logToConsole = function() {
  console.log('Hello World');
};

module.exports.logToConsole2 = function() {
};