2014-09-25 33 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' } };

爲好。