不能通过我的帖子http函数与jasmine

时间:2016-03-29 14:36:47

标签: javascript angularjs unit-testing jasmine httpbackend

我无法理解为什么我的帖子功能没有通过我的茉莉花测试。我认为在AngularJS文档中,$ httpBackend的问题非常糟糕

这是我的代码:

.controller('SystemControllerNg',
    ['sweetAlertService', 'dataExchangeService', function(sweetAlertService, dataExchangeService) {

    var self = this;
    self.all_products = [];

    /*
        Get product from database
    */
    self.getProductsFromDatabase = function() {

        var success = function(response) {
            self.all_products = response.data;
        };

        var error = function(errResponse) {
            console.log('Error getting products from database');
        };

        dataExchangeService.getProducts().then(success, error);
    };
    /*
        End get product
    */

    /*
        Send product to database
    */
    self.sendProductToDatabase = function(type) {
        var success = function(response) {
            self.getProductsFromDatabase();
        };

        var error = function(errResponse) {
            console.log('Error sending product to database');
        };

        dataExchangeService.sendProduct({
            "owner": 1,
            "name": self.product_input,
            "type": true,
            "size": 0,
            //"assign_category": self.categoryName_id
            "assign_category": null
        }).then(success, error);
    };
    /*
        End send product
    */

    /*
        Functions RUNNING in the background
    */
        self.getProductsFromDatabase();
    /*
        End functions RUNNING in the background
    */
}])

.factory('dataExchangeService', ['$http', function($http) {
    return {
        getProducts: function() {
            return $http.get('/api/v1/Products/');
        },
        sendProduct: function(dataObject) {
            return $http.post('/api/v1/Products/', dataObject);
        }
    };
}]);

并测试代码:

describe('Testing services in "Aplication" module', function () {
    beforeEach(module('Aplication'));

    describe('SystemControllerNg controller', function () {

        var ctrl, mockBackend;

        beforeEach(inject(function($controller, $httpBackend) {
            mockBackend = $httpBackend;

            ctrl = $controller('SystemControllerNg');
        }));

        //In this issue everything is ok
        it('it should load products from database', function() {
            expect(ctrl.all_products).toEqual([]);

            var resObj = {
                "owner": 1,
                "name": "",
                "type": true,
                "size": 0,
                "assign_category": null
            };

            mockBackend.expectGET('/api/v1/Products/')
                .respond([ resObj ]);

            mockBackend.flush();

            expect(ctrl.all_products).toEqual([ resObj ]);
        });

        it('it should send product to database', function() {
            //expect(ctrl.all_products).toEqual([]);

            var dataObject = {
                "owner": 1,
                "name": "product",
                "type": true,
                "size": 0,
                "assign_category": null
            };

            //I don't know why can't pass it :|
            mockBackend.expectPOST('/api/v1/Products/', dataObject).respond(201, {});

            mockBackend.flush();

            //expect(ctrl.all_products).toEqual([ dataObject ]);
        });

        afterEach(function() {
            mockBackend.verifyNoOutstandingExpectation();
            mockBackend.verifyNoOutstandingRequest();
        });
    });
});

首次测试 expectGET 工作得很好,但为什么 expectPOST 没有?如果你知道为什么它不起作用,我将非常感激。

这是我的日志控制台: console log

提前致谢。

1 个答案:

答案 0 :(得分:1)

这里有两点:

1)在您的测试中,您没有调用将调用POST事件的服务的控制器方法。

    it('it should send product to database', function() {
        //expect(ctrl.all_products).toEqual([]);

        var dataObject = {
            "owner": 1,
            "name": "product",
            "type": true,
            "size": 0,
            "assign_category": null
        };

        //I don't know why can't pass it :|
        mockBackend.expectPOST('/api/v1/Products/', dataObject).respond(201, {});
        //this is missing from your test
        ctrl.sendProductToDatabase();

        mockBackend.flush();
        scope.$digest();

        expect(ctrl.all_products).toEqual([ dataObject ]);
    });

2)当您测试调用服务方法的控制器时,您应该在测试设置期间模拟服务并将其注入控制器。您对控制器的测试应该测试控制器的功能,而不是服务的功能。独立测试您的服务。