httpBackend.expectPUT无法在angularjs工厂单元测试中工作

时间:2016-04-07 17:11:05

标签: angularjs jasmine httpbackend angular-mock

以下是我尝试进行单元测试的示例服务:

>>> s1 = set([1,2,3])
>>> s2 = set([3,4,5])
>>> s3 = set([6,7,8])
>>> len(s1.intersection(s2))
1
>>> len(s1.intersection(s3))
0

以下是我必须测试上述服务的示例规范。然而它的投掷错误

    angular.module('sampleModule')
        .factory('sampleService', sampleService);

    sampleService.$inject = ['$http'];

    function sampleService($http) {
        var endpoint = "http://localhost:8088/api/customers/"
        return {
            getData: function(id){
                return $http({
                    method: 'GET',
                    url: endpoint + id
                });
            },
            updateData: function(id, requestBodyObject){
                return $http({
                    method: 'PUT',
                    url: endpoint + id,
                    data: requestBodyObject

                });
            }
        };
    }

执行规范时出错:

    describe('Sample Service', function(){

        var service, httpBackend;

        beforeEach(function(){
            angular.mock.module('sampleModule');

            angular.mock.inject(function(_sampleService_, $httpBackend){
                service = _sampleService_;
                httpBackend = $httpBackend;
            });
        });


        it('updateAdvisorAttributesData should return response on valid acctNumber', function(){
            var response = {};
            var errResponse = {};
            var id = 1234;
            var requestBody = {
              '1': 'one'
            };
            var endpoint = "http://localhost:8088/api/customers/"

            httpBackend.expectPUT(endpoint + id, requestBody).respond(200, {'key': 'value'});

            service.updateData(id).then(function(result){
                response = result;
            },function(error){
                errResponse = error;
            });

            httpBackend.flush();
            expect(response.status).toBe(200);
            expect(response.data).toEqual({'key': 'value'});

        });
    });

相同的方法适用于httpBackend.expectGET()。然而它失败的PUT。为什么失败?我该如何解决?

1 个答案:

答案 0 :(得分:1)

当你进行测试时,你没有通过身体。

变化:

service.updateData(id).then(function(result){

到:

service.updateData(id, requestBody).then(function(result){