2015-02-23 58 views
0

我有一個私人功能,我試圖用Mocha,Chai和Sinon來模擬path.resolve()當它是私有的時,我該如何模擬path.resolve()?

現在,我得到TypeError: Arguments to path.resolve must be stringsvar projectDir = path.resolve(__dirname + "../../../");.。我不知道如何去做這件事,因爲它是私人的,我不能嘲笑它......並且它不需要參數功能,所以我不能餵它任何東西。

有什麼建議嗎?

節點腳本:

function constructDestwCallBack(absSrcFile, callback) { 
    console.log(path.resolve(__dirname + "../../../")) 
    var projectDir = path.resolve(__dirname + "../../../"); 
    ... 

function foo(callback) { 
    var destinationFile; 
    errorCheckArg(arguments); 
    return through2.obj(function(file, enc, next) { 
     destinationFile = constructDestwCallBack(absSrcFile, callback); 

測試:

describe('gulp-foo', function() { 
    var fakeFile, pspy; 

    beforeEach(function() { 
     mock({ 
      '/apple/pear/foo.less': mock.file({ 
      content: 'nothing', 
      mtime: new Date(Date.now()) 
      }) 
     }); 
     fakeFile = new File({ 
      contents: new Buffer('/apple/pear/foo.less') 
     }); 

    }); 
    afterEach(mock.restore); 


    describe('get files', function() { 

     it('should do something', function(done) { 
      var path = { resolve: function() { return "ssss"} }; 
      sinon.spy(path, "resolve"); 


      var bar = function(dest) { return dest}; 
      stream = foo(bar); 
      done(); 
     }); 
    }); 

回答

0

您可以使用rewire訪問測試的私有變量的模塊和嘲笑path.resolve功能被測模塊訪問。

相關問題