I've just looking for dependency injection in my unit test strategy using RequireJS and Jasmine. I really like the idea behind testr and I have tried to setup testr following the examples in github but I can't figure out what is wrong. I always get the error
Error: module has not been loaded: today
when testr tries to load the module that is going to be tested.
Here some context..
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);
    }
  }
});
Is there anybody that have been with the same kind of trouble? . I'm using RequireJS 2.0.6
Thanks.
Your 'today' module needs to be loaded from requirejs before you use it with testr. Try something like:
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');
      });
    });
});
Also read: http://cyberasylum.janithw.com/mocking-requirejs-dependencies-for-unit-testing/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With