带有外部依赖项的Jasmine单元测试指令失败,类型错误:'[object对象]'
Jasmine unit testing directives with external dependencies fails with TypeError: '[object Object]'
我运行规范时收到一个错误。我正在测试一个包含此服务的指令。测试返回错误消息:
TypeError:"[object object]"不是函数(正在计算"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
块中,您应该加载包含指令的应用程序模块。更改:
beforeEach(function () {
angular('RecursionHelper');
});
收件人:
beforeEach(function () {
module('MyApp'); //change to your application name.
});
此外,您应该在每次jasmine测试之前注入$compile
服务和$rootScope
。您正在使用compile
服务在不注入指令的情况下呈现指令。
只需添加到您的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();
});
相关文章:
- 错误:对象没有't支持'自动完成'
- 错误对象,本地和自定义,如何区分
- jQuery Deferred and promise-错误:对象没有't支持属性或方法'然后'
- 如何访问npm模块抛出的自定义错误对象[error:[object object]]
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- 节点发布错误对象的第一个“属性”
- 错误 [对象 BlobConstructor]' 不是 safari 中的构造函数
- JSHint 错误“对象不支持此属性或方法”用于 javascript 闭包
- 下拉 Javascript 错误:对象不支持属性或方法“匹配”
- 运行时错误:对象不支持属性或方法
- JavaScript 错误(Microsoft JScript 运行时错误:对象预期)
- 类型错误:对象 #<对象> 没有方法“身份验证”
- Javascript 错误:对象在 IE 浏览器中不支持此属性或方法
- 如果statusCode不是200,那么从http调用返回什么类型的错误对象
- 未捕获的类型错误:对象#<节点列表>没有方法'addEventListener'
- 未捕获的类型错误:对象没有方法'sortContents'
- MVC4和日期选择器:“;0x800a01b6-JavaScript运行时错误:对象没有't支持属性或方法
- Microsoft JScript运行时错误:对象没有't支持属性或方法'html'
- 未捕获的类型错误:对象#<对象>没有方法'绑定'-脊梁骨
- IE8 Javascript错误:对象没有'不支持此属性或方法