TypeError: undefined is not an object (evaluating '$httpBackend.expectPOST') in test/leave.service.tests.js (line 65)
edouard-lopez opened this issue · 6 comments
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?
you shouldn't include your test files explicity via karma.conf.js. there is an automation to load tests in this file. just leave the configs as they are checked in the repository.
let me know if this will resolve your issue.
EDIT: also the require is not necessary in the test file is not needed.
Here is how I did it
Karma-webpack: Unknown provider: LeaveServiceProvider <- LeaveService
@edouard-lopez so the issue was the angular.mock.inject. as i told you, you do not have to explicitly load the tests file because webpack.require.context can dinamically load files at runtime.
I will investigate the require.context(), thanks.
@edouard-lopez i am facing the exact problem . How did you solve it ?
In my karma file , i dont explicitly mention the spec file instead mention like this :
` files : [
'./node_modules/angular/angular.js',
'../src/main/webapp/test/unit/**/*.spec.js',
],
preprocessors: {
'../src/main/webapp/js/login.js':['webpack','sourcemap'],
'../src/main/webapp/js/mainApp.js':['webpack','sourcemap'],
'../src/main/webapp/test/unit/**/*.spec.js': ['webpack','sourcemap'],
},`
If i specifically run only single .spec file having $httpBackend . The test cases pass . But when i run everything like shown above in karma : "../src/main/webapp/test/unit/**/*.spec.js" . Then i get the same error as mentioned by you .
Can you please tell what did you do ?
@ashwanikumar415 see my stackoverflow answer.