Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $location.host()中断角度测试。如何嘲笑它?_Javascript_Angularjs_Testing_Mocking_Location - Fatal编程技术网

Javascript $location.host()中断角度测试。如何嘲笑它?

Javascript $location.host()中断角度测试。如何嘲笑它?,javascript,angularjs,testing,mocking,location,Javascript,Angularjs,Testing,Mocking,Location,我知道这里也有类似的问题,但我都试过了,都没有成功 我有一个工厂,为我的应用程序获取文本: __resolveAngularModules__('checkoutWeb.interconnections', ['ngCookies', 'angular-hal', 'LocalStorageModule']) .factory('msgData', ['$http', 'msgMock', '$location', function ($http, msgMock, $locatio

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

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

    __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”)beforeach()
中执行code>阻塞,或使用
$browser.url
,但这两种方法都不适用于我

我错过了什么?我被困在这个。。。如蒙帮助,我将不胜感激

更新: 刚试过解决方案。这个例子和我的非常相似,但解决方案仍然不起作用。我做错了什么?提前谢谢你的帮助

现在我的测试是这样的:

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();
    });
  });
  });
descripe('指令:accordionDir',函数(){
var accordionDir,element,scope,$location,$rootScope;
在每个模块之前(模块('checkoutWeb');
beforeach(注入(函数($rootScope_u$compile,$httpBackend,_msgMock,$location){
$rootScope=\u$rootScope;
scope=$rootScope;
$location=$location;
spyOn($location,'host')。andReturn('localhost');
scope.msgData=\u msgMock\u;
$httpBackend.when('GET','/api/open/text/commerce?version=1')。响应(scope.fmsData);
元素=“”;
元素=$compile(元素)(范围);
范围。$digest();
}));
它('应该调用$location主机:',函数(){
注入(函数(accordionDir){
期望($location.host()).toHaveBeenCalled();
});
});
});
Erros堆栈:

PhantomJS 1.9.8(Windows 7 0.0.0)指令:accordionDir应调用 $location host:失败类型错误:“未定义”不是对象 (正在评估'parsed.protocol') 在urlIsSameOrigin(c:/source/checkout-web/bower\u-components/angular/angular.js:14560) 在sendReq(c:/source/checkout-web/bower\u-components/angular/angular.js:8419) 在c:/source/checkout web/bower_components/angular/angular.js:8146 在c:/source/checkout web/bower\u components/angular/angular.js:11682 在c:/source/checkout web/bower\u components/angular/angular.js:11682 在c:/source/checkout web/bower_components/angular/angular.js:11768 在c:/source/checkout web/bower\u components/angular/angular.js:12811 在c:/source/checkout web/bower_components/angular/angular.js:12623 在c:/source/checkout web/src/app/additional directions/accordion-directve_test.js:18 在调用时(c:/source/checkout-web/bower\u-components/angular/angular.js:3965) 工作时fn(c:/source/checkout web/bower_components/angular mocks/angular mocks.js:2177) 未定义的错误:[$injector:unpr]未知提供程序:
accordionDirProvider无法通过$location use解决此问题。因此,我在这里获得工作测试的解决方案是在msgData工厂中使用window.location.hostname而不是$location。使用window.location.hostname,在测试中不需要额外的代码。干杯