[英]angularjs karma test typeerror $route
I am trying to test a controller for angularjs using karma, the controller inject a $route to get the current path but when I try to run karma test on it I get. 我正在尝试使用业力测试angularjs的控制器,该控制器注入$ route来获取当前路径,但是当我尝试在其上运行业力测试时,我得到了。
TypeError: 'undefined' is not an object( evaluating '$route.current')
Here is my controller: 这是我的控制器:
angular.module('myApp').controller('EditController',['$scope', '$http', '$route', function($scope,
$http,$route){
var myId = $route.current.params.myId;
$scope.var1 = 'var1';
console.log(myId);
}]);
Here is my Karma file: 这是我的业力档案:
'use strict';
describe('Controller: EditController', function(){
beforeEach(module('myApp'));
var EditCtrl,scope,route;
beforeEach(inject(function($controller,$rootScope,$route,$http){
scope=$rootScope.$new();
EditCtrl = $controller('EditCtrl',{
$scope:scope,
$route:route
});
}));
it('should have var1 equal to "var1"',function(){
expect(scope.var1).toEqual('var1');
});
});
Your beforeEach
hook isn't injecting the $route
service. 您的
beforeEach
挂钩没有注入$route
服务。 Change it to this. 更改为此。
beforeEach(inject(function($controller,$rootScope,$route,$http){
scope=$rootScope.$new();
route = $route;
EditCtrl = $controller('EditCtrl',{
$scope:scope,
$route:route
});
}));
You may also want to mock the $route.current
object in case it isn't instantiated properly, since there's no routing going on in your test. 您可能还想模拟
$route.current
对象,以防未正确实例化它,因为测试中没有进行任何路由。 In which case you could just add 在这种情况下,您可以添加
$route.current = { params: { myId: 'test' } };
in the hook as well. 在钩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.