Angular resource testing: $httpBackend.flush() cause Unexpected request

后端 未结 2 694
甜味超标
甜味超标 2021-02-06 05:25

I want to test angularjs resource.

\'use strict\';

/**
 * AddressService provides functionality to use address resource in easy way.
 *
 * This is an example us         


        
2条回答
  •  南旧
    南旧 (楼主)
    2021-02-06 05:36

    I have finally found solution:

    I tried to follow this: other post

    So I added $templateCache:

    'use strict';
    
    var $httpBackend;
    
    describe('Service: AddressService', function () {
    
        beforeEach(module('myApp'));
    
        beforeEach(inject(function ($injector, $templateChache) {
            $templateCache.put('views/home.html', '.');
            var url_get = 'api/v1/models/address/5';
    
            var response_get = {
                address_line_1: '8 Austin House Netly Road',
                address_line_2: 'Ilford IR2 7ND'
            };
    
            $httpBackend = $injector.get('$httpBackend');
    
            $httpBackend.whenGET(url_get).respond(response_get);
    
        }));
    
        describe('AddressService get test', function () {
            it('Tests get address', inject(function (AddressService) {
                var address = AddressService.get({ id: '5'});
                $httpBackend.flush();
                expect(address.address_line_1).toEqual('8 Austin House Netly Road');
                expect(address.address_line_2).toEqual('Ilford IR2 7ND');
            }));
        });
    });
    

提交回复
热议问题