我只是在我的单元测试策略中寻找使用RequireJS和Jasmine的依赖注入。我非常喜欢测试r背后的想法,我试着按照github中的例子安装testr,但我不知道哪里出了问题。我总是会犯错误
错误:模块尚未加载:今日
当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();
});spec\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');
});
});today.js .
define(['string', 'util/date'], function(string, date) {
return {
getDateString: function() {
return string.format('Today is %d', date.today);
}
}
});有谁遇到过同样的麻烦吗?我使用的是RequireJS 2.0.6
谢谢。
发布于 2012-09-21 05:03:34
在将您的“今天”模块与testr一起使用之前,需要从需求加载它。试一试如下:
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/
https://stackoverflow.com/questions/12519470
复制相似问题