Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs karma测试类型错误$route_Javascript_Angularjs_Jasmine_Karma Runner - Fatal编程技术网

Javascript angularjs karma测试类型错误$route

Javascript angularjs karma测试类型错误$route,javascript,angularjs,jasmine,karma-runner,Javascript,Angularjs,Jasmine,Karma Runner,我尝试使用karma测试angularjs的控制器,控制器注入$route以获得当前路径,但当我尝试对其运行karma测试时,我得到了 TypeError: 'undefined' is not an object( evaluating '$route.current') 这是我的控制器: angular.module('myApp').controller('EditController',['$scope', '$http', '$route', function($scope, $ht

我尝试使用karma测试angularjs的控制器,控制器注入$route以获得当前路径,但当我尝试对其运行karma测试时,我得到了

TypeError: 'undefined' is not an object( evaluating '$route.current')
这是我的控制器:

angular.module('myApp').controller('EditController',['$scope', '$http', '$route', function($scope,
$http,$route){

    var myId = $route.current.params.myId;  
    $scope.var1 = 'var1';  
    console.log(myId);
}]);
这是我的业力档案:

'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');
    }); 
});

您的
beforeach
钩子没有注入
$route
服务。换成这个

beforeEach(inject(function($controller,$rootScope,$route,$http){
 scope=$rootScope.$new();
 route = $route;
 EditCtrl = $controller('EditCtrl',{
    $scope:scope,
    $route:route
 });
}));
您可能还希望模拟
$route.current
对象,以防它没有正确实例化,因为测试中没有进行路由。在这种情况下,您可以添加

$route.current={params:{myId:'test'}


同样在钩子中。

注意EditCtrl它与EditController不同,我认为您不需要注入$route和$http