当我运行它时,我的规范出现错误。我正在测试一个包含 this service 的指令。测试返回错误消息:
TypeError: '[object Object]' is not a function (evaluating 'angular('RecursionHelper')').
不确定我缺少什么才能使其发挥作用。我正在使用 Angular 1.3 和 Jasmine 2.0。
我的指令:
return RecursionHelper.compile(element, function (scope, Element, Attrs, contro, transFn) {
//directive functions
});
我的规范:
beforeEach(function () {
angular('RecursionHelper');
});
element = angular.element('<directive></directive>');
$scope.directiveList = directiveList;
RecursionHelper.compile(element)($scope);
$scope.$digest();
最佳答案
在 beforeEach
block 中,您应该加载包含该指令的应用程序模块。变化:
beforeEach(function () {
angular('RecursionHelper');
});
致:
beforeEach(function () {
module('MyApp'); //change to your application name.
});
此外,您应该在每次 jasmine 测试之前注入(inject) $compile
服务和 $rootScope
。您正在使用 compile
服务来渲染指令而不注入(inject)它。
只需添加到您的beforeEach
:
inject(function($compile, $rootScope, $injector) {
compile = $compile;
$scope = $rootScope.$new();
});
最终代码应如下所示:
beforeEach(function () {
module('MyApp'); //change to your application name.
inject(function($compile, $rootScope, $injector) {
compile = $compile;
$scope = $rootScope.$new();
});
});
it('make an assertion', function() {
var element = angular.element('<directive></directive>');
$scope.directiveList = directiveList;
compiledElement = compile(element)($scope);
$scope.$digest();
});
关于javascript - 具有外部依赖项的 Jasmine 单元测试指令失败并出现 TypeError : '[object Object]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378920/