我让这个测试按预期工作,一切都很好:
describe('FooterComponent', () => {
let fixture;
let component;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
AppModule
],
providers: [
{provide: APP_BASE_HREF, useValue: '/'}
],
}).compileComponents();
fixture = TestBed.createComponent(FooterComponent);
component = fixture.debugElement.componentInstance;
}));
it('should create footer component', (() => {
expect(component).toBeTruthy();
}));
});
但问题是我注意到我在那里看到的大多数例子都没有导入孔应用程序模块。所以我决定稍微调查一下,并做了一些改动:
describe('FooterComponent', () => {
let fixture;
let component;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot()
],
declarations: [
FooterComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
fixture = TestBed.createComponent(FooterComponent);
component = fixture.debugElement.componentInstance;
}));
it('should create footer component', (() => {
expect(component).toBeTruthy();
}));
});
您可以看到整个项目here。
哪种解决方案更好?为什么?关于这个的最佳做法是什么?
我应该逐个导入所有模块还是只导入AppModule ......?