Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jasmine的post http功能无法通过测试_Javascript_Angularjs_Unit Testing_Jasmine_Httpbackend - Fatal编程技术网

Javascript jasmine的post http功能无法通过测试

Javascript jasmine的post http功能无法通过测试,javascript,angularjs,unit-testing,jasmine,httpbackend,Javascript,Angularjs,Unit Testing,Jasmine,Httpbackend,我不明白为什么我的post函数没有通过jasmine测试。我认为$httpBackend的问题在AngularJS文档中非常糟糕 这是我的代码: .controller('SystemControllerNg', ['sweetAlertService', 'dataExchangeService', function(sweetAlertService, dataExchangeService) { var self = this; self.all_products

我不明白为什么我的post函数没有通过jasmine测试。我认为$httpBackend的问题在AngularJS文档中非常糟糕

这是我的代码:

.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();
        });
    });
});
第一个测试expectPOST工作得很好,但为什么expectPOST不工作呢?如果你知道为什么它不起作用,我将非常感激

这是我的日志控制台:

提前感谢。

这里有两点:

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

1)谢谢,但我也尝试了
ctrl.sendProductToDatabase()之前,它仍然不起作用。我知道这是最合乎逻辑的解决方案。。2) 你可能是对的,我也会尝试一下,但对我来说没有意义,因为如果这是一个解决方案,那么函数:
mockBackend.expectGET('/api/v1/Products/).response([resObj])应该是也不起作用是吗?您是否尝试过向流程中添加摘要循环?类似于我在回答中添加的内容。您必须首先在测试设置中定义scope变量。这没有帮助,但我猜我误解了单元测试在angulajs中的工作方式。非常感谢你的帮助。我要把它写在这里,当我做对的时候,我的解决方案