【发布时间】:2015-03-04 23:04:07
【问题描述】:
我在我的 Angular 应用程序中为 lodash 库创建了一个工厂,如下所示:
angular.module('lodash',[]).service('_', function(){
var _ = window._;
// i am delete the _ property on the window
// so that i m enforced to dependency inject
// lodash each time i want to use it
// instead of using the global lodash library
delete window._;
return _;
});
现在我想测试我的模块服务,我正在使用以下测试
describe('lodash', function() {
// load the utilities module
beforeEach(module('lodash'));
describe('_', function() {
it('should be defined', inject(function(_) {
expect(_).not.toBe(null);
}));
it('should have toArray() defined', inject(function(_) {
expect(_.toArray).toBeDefined();
}));
});
});
第二次测试没有成功。完成这项工作的唯一方法是从我的服务实现中删除以下行。
delete window._;
我的问题来了:既然删除运算符只从窗口对象中删除属性而不是原始引用,为什么删除 window._ 会破坏测试并且 _ 对象在测试中为空?
【问题讨论】:
标签: angularjs jasmine karma-runner karma-jasmine lodash