Sinon.js- 试图监视 console.log 但它已经被包裹

Sinon.js- Trying to spy on console.log but it is already wrapped

我是编写节点测试的新手....这是我尝试编写的 gulp 插件的第一个单元测试:

var chai = require('chai');
var spy = require('chai-spies');
var es = require('event-stream');
var File = require('vinyl');
var mock = require('mock-fs');
var sinon = require('sinon');
var sinonChai = require("sinon-chai");
//var rewire = require("rewire");
//var myModule = rewire("./test.js");
var es = require('event-stream');

chai.should();
chai.use(sinonChai);

describe('gulp-newy', function() {
  var fs = require('fs');
    var fakeFile, pspy;

    beforeEach(function() {
        //myModule.__set__('__dirname', "/home/one");
        mock({
            __dirname: mock.directory({
                mode: 0755,
                items: {
                file1: 'file one content',
                file2: new Buffer([8, 6, 7, 5, 3, 0, 9])
                }
            })
        });

    });
    afterEach(mock.restore);


    describe('get files', function() {
        it('should do something', function(done) {
        mock({
            foo: mock.file({
            content: 'nothing',
            mtime: new Date(Date.now())
            }),
            bar: mock.file({
            content: 'nothing',
            mtime: new Date(1,1)
            })
        });
        fakeFile = new File({
           contents: new Buffer('foo'),
           history: ['foo']
        });


       var bar = function(dest) { return 'bar' };
       spy1 = sinon.spy(console, "log");
       stream = newy(bar);
       stream.write(fakeFile);
       stream.on('data', function() {
           console.log("sss");
       });
       spy1.should.have.been.called();
       done();
       });
    });
});

我得到 TypeError: Attempted to wrap log which is already wrapped,但我看不到它之前在我的间谍之前被包裹在哪里。

我正在使用 Mocha --watch...哪个会话永远不会结束。因为这个.. 另一个间谍存在。答案是Cleaning up sinon stubs easily

对于 sinon,您需要确保在每次测试后恢复 stubs\mocks。如果你不这样做,它们仍然是存根,如果你尝试在不同的测试中再次存根它们,它会大声说要再次尝试包装一个方法。 有很多方法可以做到这一点,将所有存根包含在一个沙箱中,或者只在 "after" 子句中恢复。 例如:

describe('This is a test', ()=> {


       before(()=> {
            sinon.stub(myObject,'myMethod', ()=> {
                return 'stubbed result';
            })
        });
        it('should stub my method', ()=>{
            expect(myObject.myMethod).to.be.equal('stubbed result');
        });
        after(()=> {
            //important part
            myObject.myMethod.restore();
        });
    })