2017-03-17 55 views
0

我有gulp-jasmine-phantom正在運行,但我得到了一個ReferenceError: Tictactoe is not defined。我有感覺我犯了一些根本性的錯誤。如何用gulp-jasmine,gulp-jasmine-browser或gulp-jasmine-phantom測試我的功能?

我的文件結構:

gulpfile.js 
spec/test.js 
source/js/tictactoe.js 

代碼的簡化版本:

吞:

gulp.task('site:js:test', function() { 
return gulp.src('spec/test.js') 
    .pipe(jasmine())}); 

test.js:

require("../source/js/tictactoe"); 

describe("Tictactoe", function() { 
    var t = new Tictactoe(); 
    it("expects there to be able to add a mark", function() { 
    t.addMark([0,0], 1); 
    expect(t.board[0,0]).toBe(1); 
    }); 
}); 

tictactoe.js

function Tictactoe(size) { 
    this.size = size; 

    // default variables: 
    this.EMPTY = ""; 
    this.board = []; 

    for (var i = 0; i < this.size; i++) { 
    this.board[i] = []; 
    for (var j = 0; j < this.size; j++) { 
     this.board[i][j] = this.EMPTY; 
    } 
    } 
} 

Tictactoe.prototype.addMark = function(position, player) 
{} 

我也試過gulp.src(['source/js/tictactoe.js', 'spec/test.js'])然後沒有要求在test.js。也沒有成功。我也試過gulp-jasminegulp-jasmine-browser。你能幫我做到嗎?

非常感謝!

回答

0

到你的一個相關的問題已經被回答了在這裏:How to Load a File for Testing with Jasmine Node?

總之,你需要創建一個模塊出你的井字遊戲庫,出口井字遊戲功能,然後需要到一個局部變量可以通過Jasmine訪問。 Tictactoe函數目前僅限於所需JavaScript文件的本地範圍。

相關問題