如何测试使用表单数据发布的Angular服务?

时间:2017-01-04 10:14:46

标签: angularjs testing jasmine multipartform-data

代码在浏览器中使用angular-1.5

测试

describe('Transport Cost attachment API service', function () {
    var service;
    var $httpBackend;

    beforeEach(module('app'));

    beforeEach(inject(function (_$httpBackend_, TransportCostService) {
        $httpBackend = _$httpBackend_;
        service = TransportCostService;
    }));

    it('create(): should create a transport cost attachment', function (done) {
        var transportCost = {expenseId: 123};
        var data = 'form data';
        var headers = {'Content-Type': 'multipart/form-data'};
        $httpBackend.expectPOST('/api/expense-claim/123/transport-cost-attachment/', transportCost, data, {headers: headers}).respond(201, transportCost);

        service.create(transportCost, data, {headers: headers}).then(function () {
            done();
        });
        $httpBackend.flush();
    });
});

服务

(function () {
    'use strict';

    angular.module('app')
        .factory('TransportCostService', TransportCostService);

    function TransportCostService(API, logger) {
        return {
            create: create,
        };

        function create(transportCost, data) {
            return API.TransportCostAttachment.save(transportCost, data).$promise
                .then(onComplete)
                .catch(onFail);

            function onComplete(response) {
                return response;
            }

            function onFail(error) {
                logger.error('Échec de la création, retenter dans quelques instants', null, error);
            }
        }
    }
}());

错误

PhantomJS 2.1.1 (Linux 0.0.0) Transport Cost attachment API service create(): should create a transport cost attachment FAILED
        SyntaxError: JSON Parse error: Unexpected identifier "form" in node_modules/angular/angular.js (line 1345)
        parse@[native code]
        fromJson@node_modules/angular/angular.js:1345:19
        matchData@node_modules/angular-mocks/angular-mocks.js:1940:85
        $httpBackend@node_modules/angular-mocks/angular-mocks.js:1385:33
        sendReq@node_modules/angular/angular.js:12023:21
        serverRequest@node_modules/angular/angular.js:11818:23
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        flush@node_modules/angular-mocks/angular-mocks.js:1806:45
        test/transport-cost-attachment.service.tests.js:23:21
        test/tax.service.tests.js:60:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:62:21
        test/tax.service.tests.js:49:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:52:21
        test/tax.service.tests.js:39:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:41:21
        test/tax.service.tests.js:29:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:31:21
        test/tax.service.tests.js:19:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:21:21
        test/receipt.service.tests.js:60:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:62:21
        test/receipt.service.tests.js:49:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:52:21
        test/receipt.service.tests.js:39:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:41:21
        test/receipt.service.tests.js:29:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:31:21
        test/receipt.service.tests.js:19:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:21:21
        Error: Unsatisfied requests: POST /api/expense-claim/123/transport-cost-attachment/ in node_modules/angular-mocks/angular-mocks.js (line 1843)
        verifyNoOutstandingExpectation@node_modules/angular-mocks/angular-mocks.js:1843:74
        test/transport-cost-attachment.service.tests.js:58:46
        test/tax.service.tests.js:60:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:62:21
        test/tax.service.tests.js:49:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:52:21
        test/tax.service.tests.js:39:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:41:21
        test/tax.service.tests.js:29:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:31:21
        test/tax.service.tests.js:19:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/tax.service.tests.js:21:21
        test/receipt.service.tests.js:60:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:62:21
        test/receipt.service.tests.js:49:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:52:21
        test/receipt.service.tests.js:39:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:41:21
        test/receipt.service.tests.js:29:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:31:21
        test/receipt.service.tests.js:19:8
        processQueue@node_modules/angular/angular.js:16692:30
        node_modules/angular/angular.js:16708:39
        $eval@node_modules/angular/angular.js:17990:28
        $digest@node_modules/angular/angular.js:17804:36
        $apply@node_modules/angular/angular.js:18098:31
        done@node_modules/angular/angular.js:12078:53
        handleResponse@node_modules/angular-mocks/angular-mocks.js:1369:17
        flush@node_modules/angular-mocks/angular-mocks.js:1819:37
        test/receipt.service.tests.js:21:21
PhantomJS 2.1.1 (Linux 0.0.0): Executed 168 of 170 (1 FAILED) (skipped 2) (1.107 secs / 1.213 secs)
error Command failed with exit code 1.

1 个答案:

答案 0 :(得分:0)

我的服务确实是正确的,但我的测试不是。我感到困惑,并将无用的参数传递给Aravind方法。所以

在:

expectPOST()

之后:

$httpBackend.expectPOST('/api/expense-claim/123/transport-cost-attachment/', transportCost, data, {headers: headers}).respond(201, transportCost);

测试

$httpBackend.expectPOST('/api/expense-claim/123/transport-cost-attachment/').respond(201, transportCost);