Reputation: 725
Hi I'm triying to test a custom class instantiation, i have several test in the same spec file, that's why use the beforeEach
method, also i use inject
method to get the services required by my class, but when i run the test the var appointmentCreationVehicle
are undefined
this is my code:
describe('AppointmentCreationVehicle', () => {
let appointmentCreationVehicle: AppointmentCreationVehicle;
beforeAll(() => {
TestBed.configureTestingModule({
imports: [AppModule]
})
.compileComponents();
});
beforeEach(
inject([AppointmentCreationVehicle], (vehicleRestService: VehicleRestService) => {
appointmentCreationVehicle = new AppointmentCreationVehicle(vehicleRestService);
})
);
it('should create an instance',() => {
expect(appointmentCreationVehicle).toBeTruthy();
});
then my karma.conf.js look like this:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-firefox-launcher'),
require('karma-mocha-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
jasmine: {
random: false
},
captureConsole: true,
mocha: {
bail: true
}
},
reporters: ['mocha'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['HeadlessFirefox'],
singleRun: true,
customLaunchers: {
HeadlessFirefox: {
base: 'Firefox',
flags: ['-headless']
},
ChromeDebugging: {
base: 'Chrome',
flags: ['--remote-debugging-port=9876']
}
}
});
};
It is possible than the injection of the services ends after the it execution? If i show, how can i avoid this behavior.
Upvotes: 1
Views: 588
Reputation:
You didn't import the providers into your testbed :
beforeAll(() => {
TestBed.configureTestingModule({
providers: [...] // <---------- HERE
})
.compileComponents();
});
After that, make it simpler : use the testbed ! It contains a weekMap of the dependencies :
const myServiceInstance = TestBed.get(MyService);
Upvotes: 1