2017-03-17 111 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文件的本地范围。