8

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

错误:模块尚未加载:今天

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

这里有一些上下文..

索引.html ..

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

配置.js ..

require.config({

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

主.js ..

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

规格\今天.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

谢谢。

4

1 回答 1

1

在将它与 testr 一起使用之前,需要从 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/

于 2012-09-21T05:03:34.440 回答