简体   繁体   English

使用Angular夹具。当同步稳定

[英]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:所以,总结一下:

  • When I use async and whenStable together, the test fails.当我同时使用asyncwhenStable时,测试失败。
  • When I use no async or whenStable , the test fails.当我不使用asyncwhenStable时,测试失败。
  • When I use no 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.当我不使用asyncwhenStable时,测试通过:当我对组件的 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.

相关问题 从 Angular 4 升级到 6 后,fixture.whenStable() 停止工作/调用 - fixture.whenStable() is stopped working/called after upgrading from Angular 4 to 6 角度4.3.0在执行异步函数后调用fixture.whenStable - angular 4.3.0 calling fixture.whenStable after executing an asynchronous function Angular 中 async/await 和 async/fixture.whenStable 的区别 - Difference between async/await and async/fixture.whenStable in Angular 如何在Angular 2测试中对fixture.whenStable使用wait - How to use await on fixture.whenStable in Angular 2 Tests 如果不在异步测试执行区域内,fixture.whenStable() 实际上是否在我的角度测试中执行任何操作? - Does fixture.whenStable() actually do anything in my angular tests if not within an async test execution zone? Angular Karma:从 Angular v8 升级到 v12 后,await fixture.whenStable 总是超时 - Angular Karma: await fixture.whenStable always times out after upgrade from Angular v8 to v12 测试我的组件时,夹具.whenStable()永远无法解析 - fixture.whenStable() never resolves when testing my component fixture.whenStable().then 中写入的所有预期条件都在填充,因为 SPEC 没有预期 - All expected conditions written in fixture.whenStable().then are populating as SPEC HAS NO EXPECTATIONS Angular2 whenStable()不能与observable一起使用? - Angular2 whenStable() not working with observable? Angular 测试 - 如果包含在 whenStable 中,预计不会出现 - Angular Testing - expect not seen if encompassed within whenStable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM