gpt4 book ai didi

javascript - 如何使用 gulp-jasmine、gulp-jasmine-browser 或 gulp-jasmine-phantom 测试我的功能?

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:12 32 4
gpt4 key购买 nike

我有gulp-jasmine-phantom正在运行,但我得到 ReferenceError: Tictactoe is not defined 。我感觉我犯了一些根本性的错误。

我的文件结构:

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

代码的简化版本:

gulp :

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

测试.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 。你能帮我让它工作吗?

提前非常感谢!!

最佳答案

与您相关的问题已在这里得到解答:How to Load a File for Testing with Jasmine Node?

简而言之,您需要从 tictactoe 库中创建一个模块,导出 Tictactoe 函数,然后将其放入 Jasmine 可以访问的局部变量中。 Tictactoe 函数目前只是简单地限定在所需 JavaScript 文件的本地范围内。

关于javascript - 如何使用 gulp-jasmine、gulp-jasmine-browser 或 gulp-jasmine-phantom 测试我的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42852143/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com