简体   繁体   English

如何对具有多个$ resource调用的角度控制器进行单元测试

[英]How to unit test angular controller having multiple $resource calls

I have a controller that does multiple $resource calls to the backend when initialized. 我有一个控制器,在初始化时会对后端执行多个$ resource调用。 This works fine, but now I need to add unit tests with jasmine, and here comes the issue, I get an error when I try to save the state of an object inside the controller. 这可以正常工作,但是现在我需要使用茉莉花添加单元测试,并且这里出现问题,当我尝试将对象的状态保存在控制器内部时出现错误。

At controller initialization I make 3 $resource calls to populate 3 dropdowns, after making the selections on the 3 dropdowns and input a value in another input text, I press submit(save) and my entity is saved. 在控制器初始化时,我进行了3个$ resource调用以填充3个下拉列表,在3个下拉列表上进行选择并在另一个输入文本中输入值之后,按Submit(save)并保存了我的实体。

I do not have much experience with angular so I really need help, if other resources are required just ask and I will add them to this post. 我在angular方面没有太多经验,所以我真的需要帮助,如果需要其他资源,请询问,我将它们添加到这篇文章中。 Thanks! 谢谢!

Now I need to unit test this save function and I get an error: 现在,我需要对该保存功能进行单元测试,并且出现错误:

Chrome 41.0.2272 (Windows 7) permissionController should call the service successfully FAILED
        Error: Unexpected request: POST http://localhost:8080/webapp-1.0.0-SNAPSHOT/v1/actions/search
        No more request expected
            at $httpBackend (C:/_workspace/webapp/Platform/ui/src/main/uidev/node_modules/angular-mocks/angular-mocks.js:1220:9)
            at sendReq (C:/_workspace/webapp/Platform/ui/src/main/uidev/app/lib/js/angular.js:9628:9)
            at serverRequest (C:/_workspace/webapp/Platform/ui/src/main/uidev/app/lib/js/angular.js:9344:16)
            at processQueue (C:/_workspace/webapp/Platform/ui/src/main/uidev/app/lib/js/angular.js:13189:27)
            at C:/_workspace/webapp/Platform/ui/src/main/uidev/app/lib/js/angular.js:13205:27
            at Scope.$eval (C:/_workspace/webapp/Platform/ui/src/main/uidev/app/lib/js/angular.js:14401:28)
            at Scope.$digest (C:/_workspace/webapp/Platform/ui/src/main/uidev/app/lib/js/angular.js:14217:31)
            at Function.$httpBackend.flush (C:/_workspace/webapp/Platform/ui/src/main/uidev/node_modules/angular-mocks/angular-mocks.js:1519:38)
            at Object.<anonymous> (C:/_workspace/webapp/Platform/ui/src/main/uidev/test/unit/authz/permission/create-permission-controller-test.js:28:21)

This is the controller: 这是控制器:

angular.module('platform.authz').controller('permissionController', function($translatePartialLoader,
 PermissionService, ActionService, ResourceService, EnvironmentService, $scope, $state) {

'use strict';
$translatePartialLoader.addPart('authz/permission');

var controller = this;
controller.permission = {};

function errorMessageHandler(response) {
    var errorMessages = [];
    response.data.messages.forEach(function(message, i) {
        errorMessages[i] = {type: 'danger', text: message.description};
    });
    controller.notificationMessages = errorMessages;
}

controller.populateActions = function() {
    ActionService.searchRequestObject().search({search: ''}).$promise.then(function(data) {
        controller.actions = data;
    }, errorMessageHandler);
};

controller.populateResources = function() {
    ResourceService.searchRequestObject().search({search: ''}).$promise.then(function(data) {
        controller.resources = data;
    }, errorMessageHandler);
};

controller.populateEnvironments = function() {
    EnvironmentService.searchRequestObject().search({search: ''}).$promise.then(function(data) {
        controller.environments = data;
    }, errorMessageHandler);
};

controller.getPermissionSaveRequest = function() {
    var permissionSaveRequest = {};
    permissionSaveRequest.name = controller.permission.name;
    permissionSaveRequest.actionId = controller.permission.action.id;
    permissionSaveRequest.resourceId = controller.permission.resource.id;
    permissionSaveRequest.environmentId = controller.permission.environment.id;
    return permissionSaveRequest;
};

controller.save = function() {
    PermissionService.createRequestObject().create(controller.getPermissionSaveRequest()).$promise.then(function() {
        controller.notificationMessages = [
            {type: 'success', text: 'Save has been successful!'}
        ];
        controller.permission = {};
        $scope.permissionDetailsForm.$setPristine();
    }, errorMessageHandler);
};

controller.cancel = function cancel() {
    $state.reload();
};

controller.actions = controller.populateActions();
controller.resources = controller.populateResources();
controller.environments = controller.populateEnvironments();
});

This is one of the service that is called upon controller initialization (all 3 services look the same) 这是在控制器初始化时调用的服务之一(所有3个服务看起来都一样)

angular.module('platform.authz').factory('ActionService', function($resource, $log, configService) {
'use strict';

return {
    searchRequestObject: function() {
        return $resource(configService.getUrlForService('action') + '/' + 'search', {}, {
            search: {method: 'POST', isArray: true}
        });
    }
    };
});

And this is my unit test tryout: 这是我的单元测试试用版:

describe('permissionController', function() {
'use strict';

var httpBackend, permissionController, state, scope;

beforeEach(module('platform'));

beforeEach(inject(function(_$httpBackend_, $controller, _$state_, configService, $rootScope) {
    state = _$state_;
    spyOn(state, 'go');
    spyOn(state, 'transitionTo');
    spyOn(configService, 'getUrlForService')
            .and.callFake(function(name) {
                return CONFIG.get('MAIN_URL_FOR_REST_SERVICES') + name + 's';
            }
    );
    httpBackend = _$httpBackend_;
    scope = $rootScope.$new();
    permissionController = $controller('permissionController', {$scope: scope});
}));

it('should call the service successfully', function() {
    permissionController.permission = {'name': 'all', 'action': {'id': 1, 'name': 'all'}, 'resource': {'id': 1, 'name': 'all'}, 'environment': {'id': 1, 'name': 'all'}};
    httpBackend
        .whenPOST(CONFIG.get('MAIN_URL_FOR_REST_SERVICES') + 'permissions', permissionController.getPermissionSaveRequest())
        .respond(200, 'created');
    permissionController.save();
    httpBackend.flush();
    expect(permissionController.errorMessages[0].text).toBe('Error');
    });
});

The problem is you are making 3 $resource calls when the controller is loaded, the controller is loaded when your test is executed. 问题是您在加载控制器时进行了3个$ resource调用,在执行测试时加载了控制器。 So you should make sure (in a beforeEach for example) to also mock those backend calls. 因此,您应该确保(例如在beforeEach中)也模拟那些后端调用。 You are now only mocking the one you expect when one is pressing save, but before the save event, your controller is already doing some backend communication. 现在,您仅在模拟一个按下保存时的预期,但是在保存事件之前,您的控制器已经在进行一些后端通信。 The exception you got is telling you this: "unexpected request". 您得到的例外是告诉您:“意外请求”。

fix is here: 修复在这里:

describe('permissionController', function() {
'use strict';

var $httpBackend, permissionController, scope;

beforeEach(module('platform'));

beforeEach(inject(function($injector, _$state_, configService) {
    $httpBackend = $injector.get('$httpBackend');
    scope = $injector.get('$rootScope').$new();
    scope.permissionDetailsForm = {$setPristine: function() {
    }};

    var state = _$state_;
    spyOn(state, 'go');
    spyOn(state, 'transitionTo');
    spyOn(configService, 'getUrlForService')
            .and.callFake(function(name) {
                return CONFIG.get('MAIN_URL_FOR_REST_SERVICES') + name + 's';
            }
    );

    var $controller = $injector.get('$controller');

    $httpBackend.when('POST', CONFIG.get('MAIN_URL_FOR_REST_SERVICES') + 'actions/search').respond(200,
            '[{"name": "all","id": "1"}]'
    );
    $httpBackend.when('POST', CONFIG.get('MAIN_URL_FOR_REST_SERVICES') + 'resources/search').respond(200,
            '[{"name": "all","id": "1"}]'
    );
    $httpBackend.when('POST', CONFIG.get('MAIN_URL_FOR_REST_SERVICES') + 'environments/search').respond(200,
            '[{"name": "all","id": "1"}]'
    );

    permissionController = $controller('permissionController', {$scope: scope});
    $httpBackend.flush();
}));

it('should call the service successfully', function() {
    permissionController.permission =
        {'name': 'all', 'action': {'id': 1, 'name': 'all'}, 'resource': {'id': 1, 'name': 'all'},
         'environment': {'id': 1, 'name': 'all'}};
    $httpBackend
            .whenPOST(CONFIG.get('MAIN_URL_FOR_REST_SERVICES') + 'permissions',
                permissionController.getPermissionSaveRequest())
            .respond(200, 'created');
    permissionController.save();
    $httpBackend.flush();
    expect(permissionController.notificationMessages[0].text).toBe('Save has been successful!');
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM