Angular 4 unit testing with jasmine /karma with http post mocking - how to fix

前端 未结 4 809
抹茶落季
抹茶落季 2021-01-04 23:35

I have a service I want to unit test in angular 4 typescript jasmine.

Now, the http is doing a post , and it returns an identity, however

4条回答
  •  太阳男子
    2021-01-04 23:51

    Sample test case for http service requests

    describe('Forgot Password Controller', function () {
        var $controller,
            $httpBackend,
            $q,
            $rootScope,
            $state,
            controller,
            scope,
            accountProvider;
    
        beforeEach(module('app'));
        beforeEach(inject(function (_$injector_, _$controller_, _$rootScope_) {
    
            $controller = _$controller_;
            $rootScope = _$rootScope_;
            $httpBackend = _$injector_.get('$httpBackend');
            $state = _$injector_.get('$state');
            $q = _$injector_.get('$q');
            accountProvider = _$injector_.get('accountProvider');
            scope = _$rootScope_.$new();
    
            controller = $controller(app.controllers.forgotPassword, {
                $state: $state,
                accountProvider: accountProvider
            });
        }));
    
        afterEach(function () {
            $httpBackend.verifyNoOutstandingRequest();
            $httpBackend.verifyNoOutstandingExpectation();
        });
    
        describe('forgot password submission', function () {
    
            it('Can submit a forgot password request successfully', function () {
                $httpBackend.expectPOST("URL DOMAIN" + '/events/requestPasswordReset').respond(200);
                spyOn($state, 'go');
                controller.form = { emailAddress: 'aks@gmail.com' };
    
                controller.submit();
    
                expect(controller.submitting).toBe(true);
    
                $httpBackend.flush();
    
                expect(controller.submitting).toBe(false);
                expect($state.go).toHaveBeenCalledWith('login', { successMessage: 'An email sent to ' + controller.form.emailAddress + ' contains instructions for resetting your password.' });
            });
    
            it('Can handle when a user is not found when submitting a forgot password request', function () {
                $httpBackend.expectPOST(app.env.EDGE_SERVICE_PATH + '/events/requestPasswordReset').respond(404);
                spyOn($state, 'go');
                controller.form = { emailAddress: 'aks@gmail.com' };
    
                controller.submit();
    
                expect(controller.submitting).toBe(true);
                $httpBackend.flush();
    
                // We intentionally want to make it appear to the user that the password reset email was sent even when a user
                // does not exist, to help hide info about which users exist in the system
                expect(controller.submitting).toBe(false);
                expect($state.go).toHaveBeenCalledWith('login', { successMessage: 'An email sent to ' + controller.form.emailAddress + ' contains instructions for resetting your password.' });
    
            });
    
            it('Can handle unexpected errors from submitting a forgot password request', function () {
                $httpBackend.expectPOST("URL DOMAIN"  + '/events/requestPasswordReset').respond(500);
    
                controller.submit();
                $httpBackend.flush();
    
                expect(controller.errors.unexpectedError).toBe(true);
            });
    
            it('Can handle 422 validation errors from submitting a forgot password request', function () {
                var responseData = {
                    fieldErrors: {
                        username: [{code: 'error'}, {code: 'required', message: 'This is required.'}]
                    }
                };
                $httpBackend.expectPOST("URL DOMAIN" + '/events/requestPasswordReset').respond(422, responseData);
    
                controller.submit();
                $httpBackend.flush();
    
                expect(controller.errors.validationErrors).toBe(true);
                expect(controller.errors.fieldErrors).toEqual(responseData.fieldErrors);
            });
    
            it('Can handle 503 service unavailable from submitting a forgot password request', function () {
                $httpBackend.expectPOST("URL DOMAIN" + '/events/requestPasswordReset').respond(503);
    
                controller.submit();
                $httpBackend.flush();
    
                expect(controller.errors.serviceUnavailable).toBe(true);
            });
    
        });
    
    });

提交回复
热议问题