【发布时间】:2015-04-01 07:58:54
【问题描述】:
编辑/解决方案:我找到了 $location 没有注入的答案。我需要将我的 angular-mocks.js 文件更新到最新的 1.3。一旦我解决了这个问题,它就像一个魅力!
我很难找出这段代码 sn-p 有什么问题。 我有一个登录页面,成功登录后它会将我重定向到受保护的资源页面。一切正常。期待单元测试部分。
这是我的测试
ddescribe("Login Controller", function () {
var scope, controller, location, http, ControllerService, state;
beforeEach(module('myApp'));
beforeEach(inject(function ($httpBackend, $rootScope, $controller, $location) {
scope= $rootScope.$new();
http= $httpBackend;
location = $location
ControllerService= $controller;
}));
beforeEach(inject(function ($injector) {
location = $injector.get('$location');
}));
iit("should do mock authentication", function () {
http.expectPOST('http://127.0.0.1:7000/auth').respond('True');
http.expectPOST('http://127.0.0.1:7000/obtain/').respond('4nd8n129dd01m1md8');
Newcontroller = ControllerService("simpleController", {
$location:location, $scope:scope,
tokenfactory:tokenfactory});
scope.$digest();
scope.login();
http.flush();
expect(scope.somedata).toBe('True');
});
)};
这是我的 login() 函数
var app = angular.module('myApp',['ui.bootstrap','ui.router','ngCookies', 'UserValidation','restangular']);
app.controller ('simpleController',function($scope ,$http, $location, tokenfactory,loginusercheck, usernamefactory,Restangular){
$scope.login = function(){
// some login function
//after post to login endpoint
//change the page state to something else
$location.path('list'); // where list is the url to protected page
};
})
现在当我使用 karma-jasmine 测试运行器运行它时,我得到了错误
typeError: undefined is not a function at $LocationProvider.$get
如果我注释掉 $location.path('list') 并且不在我的测试用例中注入 $location = 一切正常。 我究竟做错了什么。我似乎无法注入 $location。还有其他方法可以注入 $location 吗?
【问题讨论】:
-
编辑/解决方案:我找到了为什么 $location 没有注入的答案。我需要将我的 angular-mocks.js 文件更新到最新的 1.3。一旦我解决了这个问题,它就像一个魅力!
标签: angularjs unit-testing location karma-runner