自从我的项目中的ui-router升级到最新的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)'
这意味着正在尝试转换,但我不确定为什么这些日志来自测试。
我已将其缩小到我编写的某些组件测试,这些组件测试依赖于我在单独的核心模块中注册的帮助程序服务。因此,在这些测试中,我会像这样引入核心模块:
beforeEach(function() {
module('app.components');
module('app.core');
});
出于测试的目的,我不想模仿helper服务,因为它是单独测试的,所以我只想使用服务的真实实现,因此我在核心模块中加载。< / p>
在核心模块中,我有一个注册的默认抽象状态:
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的调用,这是我在我的业力测试运行器输出中看到的额外日志记录中引用的内容。
我想通过这个我可以在我的测试中为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迁移文档中看到他们已经进行了更改,不再吞下&#39;错误,这就是为什么我突然开始看到更详细的日志记录。
我想知道我是否应该在我的测试中为$ state服务提供我自己的defaultErrorHandler
以便不返回任何内容,但不确定这是不是很糟糕?
迁移时有没有其他人遇到过这个?
由于