【问题标题】:$location.host() breaks angular tests. How to mock it?$location.host() 打破了角度测试。如何嘲笑它?
【发布时间】:2015-06-19 07:49:59
【问题描述】:

我知道这里有类似的问题,但我都试过了,但都没有成功。

我有一个工厂,它为我的应用程序获取文本:

    __resolveAngularModules__('checkoutWeb.interconnections', ['ngCookies', 'angular-hal', 'LocalStorageModule'])
  .factory('msgData', ['$http', 'msgMock', '$location', function ($http, msgMock, $location) {
      var url;
      switch ($location.host()) {
        case 'localhost':
          url = '/texts/commerce?version=1';
          break;
        case 'www2-dev.xxx.com':
          url = '/texts/commerce?version=2';
          break;
      }
    return $http.get(url, {
      headers: {
        'ClientId': 'some-id'
      }
    }).then(function (response) {
      return response;
    }, function (response) {
      Raven.captureException(new Error("Failed to load texts with status: " + response.status + " " + response.statusText));
      return msgMock;
    });
  }]);

所以,如果我在 $http.get() 中直接使用 '/blahblah',测试是可以的,但如果我尝试使用上述逻辑,我会在测试中遇到错误。

我尝试在beforeEach() 块中注入$location 并使用spyOn($location.prototype, "host").andReturn("localhost");,或者使用$browser.url,但两者都对我不起作用。

我错过了什么?我对此感到困惑......将不胜感激任何帮助......

更新: 刚刚尝试了here 的解决方案。这个例子与我的非常相似,但解决方案仍然不起作用。我究竟做错了什么?提前感谢您的帮助。

所以我的测试现在看起来像这样:

describe('directive: accordionDir', function() {
  var accordionDir, element, scope, $location, $rootScope;

  beforeEach(module('checkoutWeb'));

  beforeEach(inject(function(_$rootScope_, $compile, $httpBackend, _msgMock_, _$location_) {

    $rootScope =_$rootScope_;
    scope = $rootScope;
    $location = _$location_;
    spyOn($location, 'host').andReturn('localhost');
    scope.msgData = _msgMock_;

    $httpBackend.when('GET', '/api/open/texts/commerce?version=1').respond(scope.fmsData);

    element ='<div accordion-dir><a href="#" class="twister"><div class="details"></div></a><div class="answer" style="display: none;"></div></div>';
    element = $compile(element)(scope);
    scope.$digest();
  }));

  it('should call $location host: ', function () {
    inject(function(accordionDir){
      expect($location.host()).toHaveBeenCalled();
    });
  });
  });

错误堆栈:

PhantomJS 1.9.8 (Windows 7 0.0.0) 指令:accordionDir 应该调用 $location host: FAILED TypeError: 'undefined' is not an object (评估'parsed.protocol') 在 urlIsSameOrigin (c:/source/checkout-web/bower_components/angular/angular.js:14560) 在 sendReq (c:/source/checkout-web/bower_components/angular/angular.js:8419) 在 c:/source/checkout-web/bower_components/angular/angular.js:8146 在 c:/source/checkout-web/bower_components/angular/angular.js:11682 在 c:/source/checkout-web/bower_components/angular/angular.js:11682 在 c:/source/checkout-web/bower_components/angular/angular.js:11768 在 c:/source/checkout-web/bower_components/angular/angular.js:12811 在 c:/source/checkout-web/bower_components/angular/angular.js:12623 在 c:/source/checkout-web/src/app/additional-directives/accordion-directve_test.js:18 在调用 (c:/source/checkout-web/bower_components/angular/angular.js:3965) 在 workFn (c:/source/checkout-web/bower_components/angular-mocks/angular-mocks.js:2177) 未定义错误:[$injector:unpr] 未知提供者: 手风琴目录提供者 http://errors.angularjs.org/1.2.28/$injector/unpr?p0=accordionDirProvider%20%3C-%20accordionDir 在 c:/source/checkout-web/bower_components/angular/angular.js:3801 在 getService (c:/source/checkout-web/bower_components/angular/angular.js:3929) 在 c:/source/checkout-web/bower_components/angular/angular.js:3806 在 getService (c:/source/checkout-web/bower_components/angular/angular.js:3929) 在调用 (c:/source/checkout-web/bower_components/angular/angular.js:3956) 在 workFn (c:/source/checkout-web/bower_components/angular-mocks/angular-mocks.js:2177) 在 c:/source/checkout-web/bower_components/angular-mocks/angular-mocks.js:2163 在 c:/source/checkout-web/src/app/additional-directives/accordion-directve_test.js:25 未定义

【问题讨论】:

    标签: javascript angularjs testing mocking location


    【解决方案1】:

    使用 $location 无法解决这个问题。所以我在这里进行工作测试的解决方案是在 msgData 工厂中使用 window.location.hostname 而不是 $location 。使用 window.location.hostname 您不需要在测试中使用额外的代码。干杯!

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 2018-04-04
      • 2021-11-30
      • 2021-02-06
      • 2015-06-06
      • 2010-11-26
      • 2016-11-07
      • 1970-01-01
      相关资源
      最近更新 更多