【发布时间】:2020-05-31 15:23:43
【问题描述】:
我是使用 Jasmine 和 Karma 在 Angular 中进行单元测试的新手。我是实习生。我正在浏览一个规范文件。我想知道在这种情况下 should create 是什么意思。 TimeselectorComponent 是我要测试的组件。我从我的前辈那里拿了代码。代码是这样的:
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { TimeselectorComponent } from './timeselector.component';
...
describe('TimeselectorComponent', () => {
let fixture: ComponentFixture<TimeselectorComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
...
],
providers: [...],
declarations: [TimeselectorComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TimeselectorComponent);
});
it('should create', () => {
expect(fixture.componentInstance).toBeTruthy();
});
// other test cases
});
还有其他测试用例,但现在我对 'should create' 用例感到好奇。我的理解是它正在尝试测试组件是否已初始化/创建。如果我错了,请纠正我。请告诉我更多关于上面的代码。也想了解compileComponent() 方法。我检查了这个链接,但只能理解一点:
compileComponents() is asynchronous, we must use async()
我也在经历这个问题:
Error: Please call “TestBed.compileComponents” before your test
【问题讨论】:
标签: angular unit-testing karma-jasmine