【问题标题】:AngularJS: $location path not defined: Unit Testing. Karma-JasmineAngularJS:$location 路径未定义:单元测试。业力茉莉
【发布时间】: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


【解决方案1】:

第一个明显的问题是,如果没有模拟位置,您就无法在单元测试中更改 $location.path。这是因为 karma 依赖于网络浏览器,而更改位置实际上会远离测试。

以下内容可能对您有所帮助:

describe("Login Controller", function () {
   var scope, controller, location, http, ControllerService, state;
    beforeEach(module('myApp'm function($provide) {
        $provide.factory('$location', {
            path: undefined
        });
    }));
    beforeEach(inject(function ($httpBackend, $rootScope, $controller) {
          scope= $rootScope.$new();
          http= $httpBackend;
          ControllerService= $controller;
    }));

  it("should do mock authentication", inject(function ($location) {
     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", {                                                                 $scope:scope,
                                            tokenfactory:tokenfactory});
        scope.$digest();
        scope.login();
        http.flush();
        expect(scope.somedata).toBe('True');
        expect($location.path).toEqual('list');
    });
))};

【讨论】:

  • 看到问题来自于将 $location 注入到 my before each 中。即使我们暂时忘记了“路径”问题,我仍然无法注入 $location。它抛出一个错误 .."undefined is not a function at LocationProvider.$get " @peter
  • 尝试像我刚刚编辑我的问题一样的东西 - 根本不要注入 $location,除了在你的 it 块中,并在模块块中模拟 $location 服务。
  • 您是否在代码中的其他任何地方使用位置?我本来期望的错误是 $locationProvider,而不是 LocationProvider,听起来你把它命名错了('Location',而不是'$location')
  • 感谢您的建议,但我找到了答案,我花了将近 2 天的时间才终于弄清楚出了什么问题。我刚刚将 angular-mocks.js 更新到 1.3
  • 不,在那个登录功能上我只有一个 $location。
猜你喜欢
  • 1970-01-01
  • 2016-06-15
  • 2014-03-12
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多