2015-10-02 48 views
0

我想單元測試一些我用Gulp構建的功能,使用Mocha我的測試框架。我是單元測試的新手,並且一直以gulp-concattesting爲例。Gulp不能在摩卡里面工作

但是,我遇到了gulp.src在與mocha一起運行時找不到任何東西的問題。我嘗試了分別從it運行確切的代碼,並且它工作正常。用mocha運行,gulp.src不輸入任何內容到流中。

var sample = Path.join(__dirname, 'sample.html'); 
describe('feature', function() { 
    it('should do something', function() { 
     gulp.src(sample) 
     .pipe((function() { 
      var stream = through.obj(function(file, enc, callback) { 
       console.log(file.path); 
       this.push(file); 
       callback(); 
      }); 
      return stream; 
     })()); 
    }); 
}); 

回答

0

你的測試是異步的,所以你必須告訴Mocha它是異步的,當你的測試結束時你必須告訴Mocha。

你告訴摩卡它是通過添加參數回調it異步:

it('should do something', function(done) { 

你告訴摩卡當測試是通過調用在適當的時候這個額外done()參數的做法。我回到你用過的例子,看到assert.end(done)用於此。所以你的代碼應該被編輯爲:

var gulp = require("gulp"); 
var path = require("path"); 
var through = require("through2"); 
var assert = require('stream-assert'); 

var sample = path.join(__dirname, 'sample.html'); 
describe('feature', function() { 
    it('should do something', function(done) { 
     gulp.src(sample) 
      .pipe((function() { 
       var stream = through.obj(function(file, enc, callback) { 
        console.log(file.path); 
        this.push(file); 
        callback(); 
       }); 
       return stream; 
      })()) 
      .pipe(assert.end(done)); 
    }); 
}); 
+0

是的,這工作完美!我以爲我不通過'done'就讓測試同步。我的錯。非常感謝;這讓我瘋狂! – midofra

+0

沒有給出傳遞給它的回調的參數只會告訴摩卡「這個測試不是異步的」。但是,如果測試中的代碼是異步的(就像您使用的代碼一樣),那麼無論您是否指定參數,此代碼都保持異步。我在[這個答案](http://stackoverflow.com/a/20760704/1906307)中進入了這些細節。 – Louis