[英]Testing Angular 1 service
I am working on an Angular 1 project where I am setting up unit testing. 我正在Angular 1项目中进行单元测试。 So, There is a service that I need to test, its name is "appSecurity".
因此,有一项我需要测试的服务,其名称为“ appSecurity”。 The methods in this service use pre-loaded user data coming from service named "userService".
该服务中的方法使用来自名为“ userService”的服务的预加载用户数据。 "appSecurity" service simply references this user data from userService to take appropriate security decisions.
“ appSecurity”服务只是从userService引用此用户数据以做出适当的安全性决定。
Below is a part appSecurity service for better understanding: 以下是appSecurity服务的一部分,可让您更好地理解:
function appSecurity($log, $http, $state, $q, localStorageService, userService, grcRoutes, appRolesConstant, controlAssessmentService) {
appSecurity.routeToErrorPageIfNotFullAccessRoleInCM = function () {
var controlRegisterRoles = _.find(userService.currentUser.controlRegisterUsers, function (controlRegisterUser) {
var controlRegisterUserRole = controlRegisterUser.applicationRoles[0];
return controlRegisterUserRole.name === appRolesConstant.FullAccess;
});
};
}
As you can see, this service has many dependencies like $log, $http, $state, $q, localStorageService, userService, grcRoutes, appRolesConstant, controlAssessmentService. 如您所见,此服务具有许多依赖项,例如$ log,$ http,$ state,$ q,localStorageService,userService,grcRoutes,appRolesConstant,controlAssessmentService。 One of them is userService which has preloaded users data.
其中之一是userService,它已预加载了用户数据。
Basically I want to have the ability to inject mocked userService in appSecurity service so that I can test its methods without worrying about its dependencies data. 基本上,我希望能够在appSecurity服务中注入模拟的userService,以便我可以测试其方法而不必担心其依赖项数据。 Below is an example to do the same for a controller.
下面是对控制器执行相同操作的示例。
beforeEach(inject(function($controller) {
$scope = $rootScope.$new();
userService = {
query: function() {
queryDeferred = $q.defer();
return {$promise: queryDeferred.promise};
}
}
spyOn(userService, 'query').andCallThrough();
// controller injected with mocked service. How can I do the same with a
//service. In this case I want to instantiate appSecurity service like this
//with inject mocked userService.
$controller('BreakfastCtrl', {
'$scope': $scope,
'userService': userService
});
}));
The way in which we can create a controller with mocked dependency injections, is there a way to create a service with mocked injections. 我们可以使用模拟的依赖项注入来创建控制器的方式是,有一种方法可以使用模拟的注入项来创建服务。 I tried $service replacing $controller in above code but it didn't work.
我尝试用$ service替换上面代码中的$ controller,但是没有用。
I want to do this, 我想做这个,
$service('appSecurityService', {
'$scope': $scope,
'userService': userService
});
But $service cannot be found in angular-mocks. 但是在角球中找不到$ service。 Any help is highly appreciated.
非常感谢您的帮助。
Thanks 谢谢
Services and controllers are created at different time, thats why you can not do it like you tried. 服务和控制器是在不同的时间创建的,这就是为什么您不能像尝试的那样去做。 At line
beforeEach(inject(function($controller) {
services are already created, like $controller
- this is also just a service. 在
beforeEach(inject(function($controller) {
处已经创建了服务,例如$controller
这也只是一个服务。
You should mock services when creating your module: 创建模块时,您应该模拟服务:
beforeEach(angular.mock.module('module', ($provide) => {
$provide.value(userService, {query: () => {}});
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.