I've a series of basic tests coded for Intern 1.4 for my code (server and client) developed on the top of Dojo Toolkit 1.9.
Now I want to test classes in isolation, with mock objects instead of the dependencies automatically resolved by the AMD loader.
Here is a set of classes with 'bb' depending on 'aa', with the mock of the 'aa' class, and the test cases I want to verify.
/* file <root>/aa.js */
define([], function() {
return {
get: function() { return 'aa' }
};
});
--
/* file <root>/bb.js */
define([ './aa' ], function(aa) {
return {
get: function() { return aa.get() + '-bb' }
};
});
--
/* file in <root>/tests/aaMock.js */
define([], function() {
return {
get: function() { return 'aaMock' }
};
});
--
/* file in <root>/tests/aaTest.js */
define([ 'intern!object', 'intern/chai!assert', '../bb', ], function (registerSuite, assert, bb) {
registerSuite({
name: 'testbed',
'bb untouched': function() {
assert.strictEqual('aa-bb', bb.get());
},
'bb with mocked aa': function () {
require(
{ map: { '*': { 'aa': '<pkg>/tests/aaMock' } } },
[ '<pkg>/bb' ],
function(bb) {
assert.strictEqual('aaMock-bb', bb.get());
}
);
}
});
});
From the test file above, Intern reports one successful test and one failure. As far as I've been able to trace the Dojo loader (Dojo 2 packaged with Intern), the second reference of the 'bb' module comes from the loader cache.
Questions:
- Is it the right approach? Or should I instrument the 'bb' class with a
injectMock()
method that will override the local reference of the 'aa' class with a given 'aaMock' reference? - I read that RequireJS caches can be invalidated with
context
andurlArgs
flags. Can we do something similar with the Dojo loader?
Note that I did not use the context-sensitive require on purpose as it does not accept a new config.
Thanks, Dom