Karma-webpack+angular TypeError: undefined is not an object (evaluating '$httpBackend.expectPOST'

半腔热情 提交于 2020-01-02 14:36:08

问题


I got the following error

PhantomJS 2.1.1 (Linux 0.0.0) leave API service create(): should create a leave FAILED
        static/app.min.js:4804:54
        forEach@static/app.min.js:440:25
        loadModules@static/app.min.js:4764:13
        createInjector@static/app.min.js:4686:31
        WorkFn@node_modules/angular-mocks/angular-mocks.js:3120:60
        loaded@http://localhost:9876/context.js:151:17
        TypeError: undefined is not an object (evaluating '$httpBackend.expectPOST') in test/leave.service.tests.js (line 65)
        test/leave.service.tests.js:65:16
        loaded@http://localhost:9876/context.js:151:17

Test

var chai = require('chai');
var assert = chai.assert;
require('../src2/pages/leaves/leave.service.js');

describe('leave API service', function () {
    var service;
    var $httpBackend;

    beforeEach(angular.mock.module('app'));

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

    it('create(): should create a leave', function (done) {
        var leave = {name: 'test'};
        $httpBackend.expectPOST('/api/leave/', leave).respond(201, leave);

        service.create(leave).then(function () {
            done();
        });
        $httpBackend.flush();
    });
});

webpack-related config in Karma.conf.js

    files: [
        './static/app.min.js',
        'node_modules/angular-mocks/angular-mocks.js',
        {pattern: 'test/leave.service.tests.js', watched: false}
    ],
    preprocessors: {
        'test/leave.service.tests.js': ['webpack']
    },

Do you know why I got trouble with the injection?

来源:https://stackoverflow.com/questions/41618626/karma-webpackangular-typeerror-undefined-is-not-an-object-evaluating-httpba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!