gpt4 book ai didi

javascript - 在 Buster.js 测试中导入其他 .js 文件

转载 作者:行者123 更新时间:2023-11-30 17:49:33 24 4
gpt4 key购买 nike

我第一次尝试使用 Buster.js 进行 Javascript 测试

我已按照 the Buster site 中的说明进行操作运行“陈述显而易见”的测试。但是,我无法将任何现有的 .js 文件导入到测试中。

例如,我有一个文件js/testLibrary.js,包含:

function addTwo(inp) {
return inp+2;
}

和一个文件test/first-test.js,包含:

// Node.js tests
var buster = require("buster");
var testLibrary = require("../js/testLibrary.js");
var assert = buster.referee.assert;

buster.testCase("A module", {
"Test The Library": function() {
result = addTwo(3);
console.log(result);
assert(true, 'a message for you');
}
});

运行 buster-test 给出:

Error: A module Test The Library
ReferenceError: addTwo is not defined
[...]

result = addTwo(3); 替换为 result = testLibrary.addTwo(3); 得到:

Error: A module Test The Library
TypeError: Object #<Object> has no method 'addTwo'
[...]

我可能遗漏了一些非常基本的东西,但目前,我完全被难住了。有人可以指出我正确的方向吗?

最佳答案

那是因为你没有从模块中导出这个函数。看看那个: http://nodejs.org/api/modules.html#modules_module_exports

关于javascript - 在 Buster.js 测试中导入其他 .js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19345052/

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