我最近从Angular 5升级到Angular 6,并开始使用cli。在我仅将Webpack与茉莉花和业力一起使用来运行单元测试之前。我通常编写单元测试的方式是这样的:
describe('ComponentToTest', () => {
beforeEach(() => {
this.injector = ReflectiveInjector.resolveAndCreate([
{ provide: DALService, useClass: MockDal },
RealService
]);
this.datePipe = new DatePipe('en-US');
this.component2 = this.injector.get(Component2);
this.dal = this.injector.get(DALService);
this.realService = this.injector.get(RealService);
this.componentToTest = new ComponentToTest(
this.datePipe,
this.component2,
this.dal,
this.realService);
// Test Data
this.testData = [1, 2, 3];
});
describe('testFunctionOne', () => {
it('should return true', () => {
expect(this.componentToTest.testFunctionOne()).toEqual(true);
});
});
});
但是对于cli项目,当我运行“ ng test”时,所有测试都失败,并说“ this”未定义。例如。实际错误之一是:
TypeError: Cannot set property 'injector' of undefined
我可以在测试中不再使用'this'吗?一切都必须是在main describe内部声明的变量吗?