Implement JavaScript dependencies using RequireJS, Jasmine, and testr

I'm only looking for dependency injection in my unit test strategy using RequireJS and Jasmine. I really like the idea of testr , and I tried installing testr following the examples on github, but I cannot figure out what is wrong, I always get an error

Error: module was not loaded: today

when testr tries to load the module to be tested.

Here is the 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(); }); 

specifications \ 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); } } }); 

Does anyone have the same problems ?, I'm using RequireJS 2.0.6

Thanks.

+8
dependency-injection unit-testing requirejs jasmine
source share
1 answer

The module 'today' must be loaded from requirejs before using 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/

+1
source share

All Articles