Brk*_*Brk 4 jasmine testbed karma-jasmine angular angular-test
嘿,我是 angular 6(又名 angular)测试的新手,我有一个问题,就是要重新评估迄今为止我看到的每一个测试。
我们先来看看简单组件的简单测试(由cli生成)
describe('CompComponent', () => {
let component: CompComponent;
let fixture: ComponentFixture<CompComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CompComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CompComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
我有一个主要问题:
1.我怎么知道每个 Async beforeEach 调用是在每个测试单元(又名它)之前完成的?是否有任何情况下此调用会在每个它之后发生,因为它毕竟是异步调用?
每个都beforeEach将在任何测试开始之前完成。
在您的示例中,compileComponents()是异步的。因此,在这种情况下我们必须使用 async() 方法。
供您参考,请查看此链接:https : //github.com/angular/angular-cli/issues/4774
为确保您beforeEach将在任何测试开始之前完成,您可以尝试通过添加以下内容来调试您的测试:
compileComponents().then( result => {
console.log(result);
}
Run Code Online (Sandbox Code Playgroud)
你可以在这一行设置一个断点:console.log(result);并且你会看到它会在你运行测试时一直执行,所以如果你在console.log' line and another one in your firt它的test, you will see you will never get to the second break point before doing theconsole.log`断点 1 中设置一个断点,这意味着我们将不得不在进行任何测试之前,等待 beforeEach 中的任何异步调用。
另一种查看beforeEachwill all time 在任何测试开始之前完成的方法也是以这种方式编写测试:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CompComponent ]
}).compileComponents().then( result => {
fixture = TestBed.createComponent(CompComponent);
component = fixture.componentInstance;
}
}));
Run Code Online (Sandbox Code Playgroud)
你会看到在你的任何测试fixture中已经可用,这样做。所以你不需要添加另一个 beforeEach 来初始化你的fixture.
要了解更多信息,您可以参考 Stack Overflow 上的另一个答案:
angular-2-testing-async-function-call-when-to-use
| 归档时间: |
|
| 查看次数: |
5848 次 |
| 最近记录: |