最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

I am new to writing node tests.... this is a my first unit test for a gulp plugin I am trying to write:

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();
       });
    });
});

I get TypeError: Attempted to wrap log which is already wrapped, but I don't see where it was previously wrapped before my spy.

I am new to writing node tests.... this is a my first unit test for a gulp plugin I am trying to write:

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();
       });
    });
});

I get TypeError: Attempted to wrap log which is already wrapped, but I don't see where it was previously wrapped before my spy.

Share Improve this question asked Mar 14, 2015 at 19:58 dmandman 11.1k25 gold badges116 silver badges217 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I was using Mocha --watch...which session never ends. Because of this.. the other spy existed. Answer is Cleaning up sinon stubs easily

With sinon you need to make sure you restore your stubs\mocks after every test. if you don't they remain stubs and if you try and stub them again in a different test it will shout about trying to wrap a method again. There are many ways to do this, contain all of the stubs within a sandbox, or just restore in the "after" clause. for example:

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();
        });
    })
发布评论

评论列表(0)

  1. 暂无评论