Javascript 使用require.js和testr.js启动并运行
我在启动和运行用于模拟依赖项的程序时遇到了一些问题。我有以下目录结构:Javascript 使用require.js和testr.js启动并运行,javascript,unit-testing,dependency-injection,requirejs,Javascript,Unit Testing,Dependency Injection,Requirejs,我在启动和运行用于模拟依赖项的程序时遇到了一些问题。我有以下目录结构: <root> |-- Scripts | |-- lib | | +-- require.js | +-- modules | |-- dependency.js | +-- testable-thing.js |-- Test | |-- lib | | +-- testr.js | +--
<root>
|-- Scripts
| |-- lib
| | +-- require.js
| +-- modules
| |-- dependency.js
| +-- testable-thing.js
|-- Test
| |-- lib
| | +-- testr.js
| +-- index.html
+-- index.html
请求http:///index.html
使用require config.js
(下面的源代码)可以正常工作并登录到控制台:
我还有入口点http:///test/index.html
,它将在一个完整的应用程序中运行JavaScript单元测试。看起来是这样的:
testr.config({
根:“..”
});
testr.run(“/scripts/require config.js”,函数(){
控制台日志(“进入测试”);
var sut=testr(“模块/可测试的东西”{
“模块/依赖项”:函数(){
控制台日志(“加载的存根”);
}
});
});
这就是我遇到麻烦的地方。它给出以下输出:
现在,我知道testr.js覆盖了RequireJS的require
方法来注册加载的模块,并用传递给testr
函数的存根/模拟来覆盖它们,但我一辈子都无法确定如何“加载”这些依赖项。如果我在test/index.html
中修改testr.run
回调例程,以包含一些不加载依赖项的内容:
testr.run(“/scripts/require config.js”,函数(){
控制台日志(“进入测试”);
require([“模块/可测试的东西”],函数(){});
var sut=testr(“模块/可测试的东西”{
“模块/依赖项”:函数(){
控制台日志(“加载的存根”);
}
});
});
然后发生这种情况:
我真的不知道为什么进入测试
会在这里打印两次。它只出现在源代码中的那个地方
这是我的/index.html
和我的/scripts/require config.js
:
///index.html
要求([“模块/可测试件”],功能(可测试件){
});
///scripts/require-config.js
require.config({
baseUrl:“/scripts”
});
我如何开始模拟这些依赖项并运行它们?请求/test/index.html
时,我想查看:
entering tests
stub loaded
testable thing loaded
在Chrome控制台中。我有一件事稍有差错。在
http:///tests/index.html
:
require(["modules/testable-thing"], function () {
console.log("entering tests");
var sut = testr("modules/testable-thing", {
"modules/dependency": {
run: function () {
console.log("stub loaded");
}
}
});
});
注意当回调传递到require()
时测试是如何运行的。作者Janith对此很有帮助
此外,当前版本的testr中似乎存在一个bug。使用latest()我们得到以下输出:
然而,使用(Boilerplatejs使用的)我们取得了成功:
今晚我会做更多的调查,看看这是怎么回事