文档中的所有测试在执行每个测试之前都要重新配置测试平台。当然,以下内容必须更有效,并且意味着测试运行速度会大大提高。
由于某些原因,由于区域错误,beforeAll设置测试台和beforeEach创建夹具均不起作用,但是嵌套的描述模式起作用。
describe('The shared form component:', () => {
let fixture: ComponentFixture<FormComponent>;
let component: FormComponent;
let httpClient: HttpClient;
let httpTestingController: HttpTestingController;
// setup testbed before all tests run to improve tests speed
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
SharedTestModule,
FormsModule,
ReactiveFormsModule,
],
declarations: [
DateFieldsInputComponent,
PasswordValidatorDirective,
FormComponent,
],
providers: [
{ provide: ApiBaseService, useValue: {} }
]
}).compileComponents();
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
}));
describe('A basic form with text input fields', () => {
beforeEach(async(() => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
}));
afterEach(() => {
// After every test, assert that there are no more pending requests.
httpTestingController.verify();
});
it('should create the component', () => {
expect(component).toBeTruthy();
});
});
});