$ location.host()打破了角度测试。如何嘲笑它?

时间:2015-06-19 07:49:59

标签: javascript angularjs testing mocking location

我知道这里有类似的问题,但我已经尝试过所有这些都没有成功。

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

    __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',测试就可以了,但如果我正在尝试使用上述逻辑,我会在测试中遇到错误。

我尝试在$location块中注入spyOn($location.prototype, "host").andReturn("localhost");并使用beforeEach(),或使用$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();
    });
  });
  });

Erros堆栈:

  

PhantomJS 1.9.8(Windows 7 0.0.0)指令:accordionDir应该调用   $ location host:FAILED TypeError:'undefined'不是对象   (评估'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)         at workFn(c:/source/checkout-web/bower_components/angular-mocks/angular-mocks.js:2177)     undefined错误:[$ injector:unpr]未知提供者:   accordionDirProvider&lt; - accordionDir     http://errors.angularjs.org/1.2.28/ $注射器/ unpr?P0 = accordionDirProvider%20%3 C-%20accordionDir         在c:/source/checkout-web/bower_components/angular/angular.js:3801         at getService(c:/source/checkout-web/bower_components/angular/angular.js:3929)         在c:/source/checkout-web/bower_components/angular/angular.js:3806         at getService(c:/source/checkout-web/bower_components/angular/angular.js:3929)         在调用时(c:/source/checkout-web/bower_components/angular/angular.js:3956)         at 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     未定义

1 个答案:

答案 0 :(得分:0)

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