2014-09-25 63 views
0

我想测试一个控制器为angularjs使用业力,控制器注入$路线获取当前路径,但是当我尝试运行业绩测试时,我得到。

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

小心EditCtrl与EditController不一样我认为你不需要注入$ route和$ http – Whisher 2014-09-25 15:56:20

回答

2

beforeEach钩不注入$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' } };

为好。