[英]Using Angular fixture.whenStable synchronously
I have a synchronous test of the form:我有一个形式的同步测试:
it('should do things' () => {
const compiled = fixture.debugElement.nativeElement;
button = compiled.querySelector('#buttonid');
button.click();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(state).toBe(whatIwant);
}
button.click();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(state).toBe(whatIwant);
}
});
It works, but I don't understand why.它有效,但我不明白为什么。
Everything I read online indicates that whenStable
should only be used in an async
test, but the above test does not wrap the testing function with the async() wrapper.我在网上阅读的所有内容都表明whenStable
只能用于async
测试,但上述测试并未使用 async() 包装器包装测试 function。 If I do that, the test fails (that is, the expects fail because those changes to not appear to be reflected in the component yet).如果我这样做,测试会失败(也就是说,预期会失败,因为这些更改似乎还没有反映在组件中)。
According to another answer here, using the whenStable
method in a synchronous test is pointless: Does fixture.whenStable() actually do anything in my angular tests if not within an async test execution zone?根据此处的另一个答案,在同步测试中使用whenStable
方法是没有意义的: 如果不在异步测试执行区域内,fixture.whenStable() 实际上在我的 angular 测试中做任何事情吗?
No the whenStable() does nothing if you test without async of fakeAsync.不,如果您在没有 fakeAsync 异步的情况下进行测试,那么 whenStable() 什么也不做。 What whenStable() does is to wait for all tasks in the test NgZone to complete. whenStable() 所做的是等待测试 NgZone 中的所有任务完成。 When you don't test with async the NgZone does not get created at all and whenStable() just returns immediately.当您不使用异步进行测试时,根本不会创建 NgZone 并且 whenStable() 会立即返回。
However, if I take their word for it, and I omit the whenStable
wrapper, and just make the assertions after calling fixture.detectChanges(), the test fails.但是,如果我相信他们的话,并且我省略了whenStable
包装器,并且只是在调用 fixture.detectChanges() 之后做出断言,测试就会失败。
So, to summarize:所以,总结一下:
async
and whenStable
together, the test fails.当我同时使用async
和whenStable
时,测试失败。async
or whenStable
, the test fails.当我不使用async
或whenStable
时,测试失败。async
and whenStable
, the test passes : by the time I make each check on the state of the component, the state has been updated to what I expect.当我不使用async
和whenStable
时,测试通过:当我对组件的 state 进行每次检查时,state 已更新为我所期望的。 This test only passes when I use whenStable
.此测试仅在我使用whenStable
时通过。 What I'd like to know is: why?我想知道的是:为什么?
So, it seems as though, in contradiction to the accepted answer on the above question, whenStable is doing something here.因此,似乎与上述问题的公认答案相反,whenStable 在这里做某事。 What is that thing, exactly?那到底是什么东西? Does the context I've omitted here (what happens upon clicking a button, etc.) matter?我在这里省略的上下文(单击按钮等会发生什么)重要吗?
According to the Angular documentation: https://angular.io/guide/testing根据 Angular 文档: https://angular.io/guide/testing
The fixture.whenStable() returns a promise that resolves when the JavaScript engine's task queue becomes empty.当 JavaScript 引擎的任务队列变空时,fixture.whenStable() 返回一个 promise 解析。
I'm unsure what this means exactly: can anybody clarify?我不确定这到底是什么意思:有人可以澄清吗?
This test only passes when I use
whenStable
.此测试仅在我使用whenStable
时通过。 What I'd like to know is: why?我想知道的是:为什么?
Well, that is most probably because the tests execute and finish before the async operations inside the whenStable
execute (and fail), so you end up with a test with no expectations (and thus no possibility to fail).嗯,这很可能是因为测试在whenStable
中的异步操作执行(并且失败)之前执行和完成,所以你最终会得到一个没有期望的测试(因此没有失败的可能性)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.