8

RequireJS と Jasmine を使用して、単体テスト戦略で依存性注入を探しています。私はtestrの背後にあるアイデアが本当に好きで、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');
  });
});

今日.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 に答える