2015-12-20 18 views
2

我在运行时遇到了错误。我正在测试一个包含this service的指令。测试返回错误消息:具有外部依赖关系的Jasmine单元测试指令失败,出现TypeError错误:'[object Object]'

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(); 
+0

它是否应该被CONTRO或控制 – Manu

+0

嗨马努,CONTRO,不使用。 – Jimi

回答

2

beforeEach块,你应该载入您的应用程序模块,它包含了指令。变化:

beforeEach(function() { 
    angular('RecursionHelper'); 
}); 

要:

beforeEach(function() { 
    module('MyApp'); //change to your application name. 
}); 

此外,你应该注入每一茉莉测试之前$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(); 
}); 
+0

嗨,谢谢你回答我的问题。它非常接近。我做了调整。我得到错误:\t TypeError:'undefined'不是一个对象(评估'RecursionHelper.compile') – Jimi

+0

请注意,我已经删除了这行'RecursionHelper.compile(element)($ scope);'并且添加了' compiledElement = compile(element)($ scope);' –

+0

ok。所以我们使用的是Angular服务而不是RecursionHelper函数? – Jimi

相关问题