我有一个angularjs指令,需要一个资源属性来定义,以便进行一些逻辑。下面是它的一个最小定义:
angular.module('myAppName')
.directive('loadObjects', loadObjects);
function loadObjects($window) {
var directive = {
restrict: 'E',
template: '<md-content class="md-whiteframe-z4" layout-padding></md-content>',
link: function(scope, element, attrs) {
if (angular.isUndefined(attrs.resource)) {
throw 'resource attr is mandatory';
}
}
};
return directive;
}
那么在我的测试中,我试着测试一个无效的模板是否会抛出异常。
describe('LoadObjects directive', function() {
beforeEach(module('myAppName'));
it('should throw an error if resource attr is not defined', inject(function($rootScope, $compile) {
var scope = $rootScope.$new();
var elem = angular.element('<load-objects></load-objects>');
expect(function() { $compile(elem)(scope); }).toThrow('resource attr is mandatory');
}));
});
但是我得到了下一个错误:
Expected function to throw 'resource attr is mandatory', but it threw TypeError: 'undefined' is not an object (evaluating '$window.Raven.captureMessage')
我也试过在模板中使用该指令,我可以在javascript控制台看到异常。
欢迎提出任何建议。谢谢你的阅读。
编辑
之后,我试着使用:
beforeEach(module('myAppName', function($provide, $injector) {
$provide.constant('DEBUG', true);
console.log($injector.get('DEBUG')); // it prints true
}));
在beforeEach中,DEBUG被修改了,但它不起作用。
为了我的测试成功,我需要DEBUG=true
,我的第二个问题是:这种方法有什么问题?
好的,这是我的最后一个方法:
正如我所说,如果我配置:
,我的测试将通过$ravenProvider.development(DEBUG); // with DEBUG=true
但由于:
beforeEach(module('myAppName', function($provide, $injector) {
$provide.constant('DEBUG', true);
console.log($injector.get('DEBUG')); // it prints true
}));
不能工作,我决定直接改变传递给raven的值:
beforeEach(module('myAppName', function($ravenProvider) {
$ravenProvider.development(true);
}));
好了,现在可以了