Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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
Angularjs 升级ui路由器后,karma测试运行输出中出现转换拒绝错误日志_Angularjs_Angular Ui Router - Fatal编程技术网

Angularjs 升级ui路由器后,karma测试运行输出中出现转换拒绝错误日志

Angularjs 升级ui路由器后,karma测试运行输出中出现转换拒绝错误日志,angularjs,angular-ui-router,Angularjs,Angular Ui Router,自从将我的项目中的ui路由器升级到最新的1.0.x版本以来,我开始在运行Jasmine单元测试时看到控制台上有一些额外的日志记录 LOG: 'Transition Rejection($id: 1 type: 6, message: The transition errored, detail: Error: [$injector:unpr] Unknown provider: AuthServiceProvider <- AuthService http://errors.angular

自从将我的项目中的ui路由器升级到最新的1.0.x版本以来,我开始在运行Jasmine单元测试时看到控制台上有一些额外的日志记录

LOG: 'Transition Rejection($id: 1 type: 6, message: The transition errored, detail: Error: [$injector:unpr] Unknown provider: AuthServiceProvider <- AuthService
http://errors.angularjs.org/1.6.3/$injector/unpr?p0=AuthServiceProvider%20%3C-%20AuthService)'
出于测试的目的,我不想模拟helper服务,因为它是单独测试的,所以我只想使用服务的实际实现,因此我要加载核心模块

在核心模块中,我注册了一个默认的抽象状态:

import { AuthenticationService } from './../dataservices/authentication/authentication.service';

export const initialise = () => {
  const checkUserAccess = (AuthService: AuthenticationService) => {
    return AuthService.hasAccessToken();
  };
  checkUserAccess.$inject = ['AuthService'];

  angular.module('app.core').config(['$stateProvider', '$urlRouterProvider', '$locationProvider', ($stateProvider, $urlRouterProvider, $locationProvider) => {
    $locationProvider.hashPrefix('');
    $stateProvider.state('common', {
      templateUrl: 'common.html',
      abstract: true,
      resolve: {
        checkUserAccess: checkUserAccess
      }
    }).state('home', {
      url: '/',
      templateUrl: 'app/features/home/page/home_page.html',
      controller: 'HomePageController',
      controllerAs: 'vm',
      parent: 'common'
    });
    $urlRouterProvider.otherwise('/');
  }]);
};
如您所见,这依赖于解析对AuthService的调用,这是我在karma test runner输出中看到的附加日志中引用的内容

我想,为了克服这个问题,我可以在测试中为AuthService提供一个模拟工厂,如下所示:

beforeEach(function() {
    module('app.components');
    module('app.core');
    module(function($provide) {
      $provide.factory('AuthService', function() {
        return {
          hasAccessToken: function() {
            var deferred = $q.defer();
            deferred.resolve();
            return deferred.promise;
          }
        };
      });
    });
  });
添加此项后,我看到的日志消息将更改为:

LOG: 'Transition Rejection($id: 1 type: 6, message: The transition errored, detail: Error: [$compile:tpload] Failed to load template: common.html (HTTP status: undefined undefined)
http://errors.angularjs.org/1.6.3/$compile/tpload?p0=common.html&p1=undefined&p2=undefined)'
这与我的核心模块抽象状态中的状态有关,但我对是什么导致调用此状态转换感到有点困惑。我的所有测试都没有调用$state.go(),所以这只是在app.core模块中添加的行为,当文件加载到浏览器中时(我使用的是PhantomJS),它会导航到默认的根url,然后查找注册的任何状态,并在我的核心模块中找到这些状态

我猜答案是肯定的,因为我现在可以清理日志了,我在测试文件中看到我添加模板的期望值,我将分别加载到app.core模块中:

$httpBackend.expectGET('common.html').respond({});
$httpBackend.expectGET('app/features/home/page/home_page.html').respond({});
我确实从ui router 1.0迁移文档中看到了他们所做的更改,不再“吞没”错误,这就是为什么我突然开始看到更详细的日志记录

我想知道我是否应该在测试中在$state服务上提供自己的
defaultErrorHandler
,不返回任何内容,但不确定这是否是一种错误的做法

迁移过来时,有没有其他人遇到过这种情况

谢谢

$httpBackend.expectGET('common.html').respond({});
$httpBackend.expectGET('app/features/home/page/home_page.html').respond({});