我知道这是一篇较旧的帖子,但我想我会回答,因为我找到了今天帮助我解决同样问题的解决方案。我有一个 jasmine 单元测试,可确保 dcjqaccordion 插件在包装手风琴插件的 AngularJS 提供程序中使用一组默认值进行初始化。我所做的是我用一个间谍来模拟我传递给插件的元素,该间谍将预期的插件调用的函数存根。
测试代码:
var mockElement = jasmine.createSpyObj('mockElement', ['dcAccordion']);
it('should initialize with prescribed defaults', function(){
var expectedDefaults = {
eventType: 'click',
autoClose: false,
saveState: true,
disableLink: true,
speed: 'slow',
showCount: false,
autoExpand: true,
classExpand: 'dcjq-current-parent'
};
testDcjqaccordionProvider.initialize(mockElement);
expect(mockElement.dcAccordion).toHaveBeenCalledWith(expectedDefaults);
});
生产代码:
app.provider('dcjqaccordionWrapper', function(){
//Lazy load our dependency on the plugin
function getAccordionPlugin(){
$.getScript('scripts/jquery.dcjqaccordion.2.7.js')
.done(function(){
console.log("Successfully loaded the dcjqaccordion plugin.");
})
.fail(function(){
console.log("There was a problem loading the dcjqaccordion plugin.");
});
}
var defaults = {
eventType: 'click',
autoClose: false,
saveState: true,
disableLink: true,
speed: 'slow',
showCount: false,
autoExpand: true,
classExpand: 'dcjq-current-parent'
};
this.initialize = function(inElement){
inElement.dcAccordion(defaults);
};
this.$get = function(){
getAccordionPlugin();
return this;
};
})