gpt4 book ai didi

unit-testing - 使用 RequireJS、Jasmine 和测试的 JavaScript 依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-04 08:52:54 26 4
gpt4 key购买 nike

我只是在使用 RequireJS 和 Jasmine 的单元测试策略中寻找依赖注入(inject)。我真的很喜欢 testr 背后的想法我尝试按照 github 中的示例设置 testr,但我不知道出了什么问题。我总是得到错误

Error: module has not been loaded: today



当 testr 尝试加载要测试的模块时。

这里有一些上下文..

index.html
..
<script data-main="config" src="../../assets/js/libs/require.js"></script>
<script src="vendor/testr.js"></script>

config.js
..
require.config({

// Initialize specs.
deps:["main"],
...
...
});

main.js
..
require([
// Load the example spec, replace this and add your own spec
"spec/today"
], function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.execute();
});

规范\today.js
..
describe('Today print', function() {
var date = {}, today;
beforeEach(function() {
date.today = new Date(2012, 3, 30);
today = testr('today', {'util/date': date}); //Here is where the error is thrown
});

it('is user-friendly', function() {
expect(today.getDateString()).toBe('Today is Monday, 30th April, 2012');
});
});

今天.js
..
define(['string', 'util/date'], function(string, date) {
return {
getDateString: function() {
return string.format('Today is %d', date.today);
}
}
});

有没有人遇到过同样的问题? .我正在使用 RequireJS 2.0.6

谢谢。

最佳答案

您的“今天”模块需要先从 requirejs 加载,然后才能用于测试。
尝试类似:

require(['today'], function(){
describe('Today print', function() {
var date = {}, today;
beforeEach(function() {
date.today = new Date(2012, 3, 30);
today = testr('today', {'util/date': date}); //Here is where the error is thrown
});

it('is user-friendly', function() {
expect(today.getDateString()).toBe('Today is Monday, 30th April, 2012');
});
});
});
另请阅读: http://cyberasylum.janithw.com/mocking-requirejs-dependencies-for-unit-testing/

关于unit-testing - 使用 RequireJS、Jasmine 和测试的 JavaScript 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519470/

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