【发布时间】:2019-05-29 11:21:37
【问题描述】:
您好,我正在尝试测试自定义类实例化,我在同一个规范文件中有多个测试,这就是为什么使用 beforeEach 方法,我也使用 inject 方法来获取我的类所需的服务,但是当我运行测试时,var appointmentCreationVehicle 是未定义的
这是我的代码:
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();
});
然后我的 karma.conf.js 看起来像这样:
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']
}
}
});
};
有可能在它执行之后服务的注入就结束了吗?如果我显示,我该如何避免这种行为。
【问题讨论】:
标签: angular karma-jasmine