如何在 RequireJS 中模拟单元测试的依赖项?
我有一个要测试的 AMD 模块,但我想模拟它的依赖项而不是加载实际的依赖项.我正在使用 requirejs,我的模块的代码如下所示:
I have an AMD module I want to test, but I want to mock out its dependencies instead of loading the actual dependencies. I am using requirejs, and the code for my module looks something like this:
define(['hurp', 'durp'], function(Hurp, Durp) {
return {
foo: function () {
console.log(Hurp.beans)
},
bar: function () {
console.log(Durp.beans)
}
}
}
如何模拟 hurp
和 durp
以便有效地进行单元测试?
How can I mock out hurp
and durp
so I can effectively unit test?
所以在阅读了这篇文章后 我想出了一个解决方案,它使用 requirejs 配置函数为您的测试创建一个新的上下文,您可以在其中简单地模拟您的依赖项:
So after reading this post I came up with a solution that use the requirejs config function to create a new context for your test where you can simply mock your dependencies:
var cnt = 0;
function createContext(stubs) {
cnt++;
var map = {};
var i18n = stubs.i18n;
stubs.i18n = {
load: sinon.spy(function(name, req, onLoad) {
onLoad(i18n);
})
};
_.each(stubs, function(value, key) {
var stubName = 'stub' + key + cnt;
map[key] = stubName;
define(stubName, function() {
return value;
});
});
return require.config({
context: "context_" + cnt,
map: {
"*": map
},
baseUrl: 'js/cfe/app/'
});
}
因此它创建了一个新的上下文,其中 Hurp
和 Durp
的定义将由您传递给函数的对象设置.名称的 Math.random 可能有点脏,但它有效.因为如果你有一堆测试,你需要为每个套件创建新的上下文,以防止重用你的模拟,或者在你想要真正的 requirejs 模块时加载模拟.
So it creates a new context where the definitions for Hurp
and Durp
will be set by the objects you passed into the function. The Math.random for the name is maybe a bit dirty but it works. Cause if you'll have a bunch of test you need to create new context for every suite to prevent reusing your mocks, or to load mocks when you want the real requirejs module.
在您的情况下,它看起来像这样:
In your case it would look like this:
(function () {
var stubs = {
hurp: 'hurp',
durp: 'durp'
};
var context = createContext(stubs);
context(['yourModuleName'], function (yourModule) {
//your normal jasmine test starts here
describe("yourModuleName", function () {
it('should log', function(){
spyOn(console, 'log');
yourModule.foo();
expect(console.log).toHasBeenCalledWith('hurp');
})
});
});
})();
所以我在生产中使用这种方法有一段时间了,它非常强大.
So I'm using this approach in production for a while and its really robust.