简体   繁体   English

Jasmine异步测试问题

[英]Jasmine async testing issue

I'm using Jasmine in my end-2-end tests written with protractor. 我在使用量角器编写的最终2端测试中使用了Jasmine。 I'm trying to create page object abstraction to be used as base class for other page objects defined in my solution. 我正在尝试创建页面对象抽象,以用作我的解决方案中定义的其他页面对象的基类。

As my application is classic form based solution, so I can gather common behaviors for edit pages into base class. 由于我的应用程序是基于经典表单的解决方案,因此我可以将编辑页面的常见行为收集到基类中。 To be more closer to M** pattern - I want to connect my abstract view with concrete model. 为了更接近M **模式 - 我想将我的抽象视图与具体模型联系起来。

So my base page object view looks like: 所以我的基页对象视图如下所示:

export class EditPageBase<T> {
   constructor(T model) {
      this.model = model;
   }

   fillForm() {
      ...
   }

   checkForm() {
      ...
   }
}

where T - class model related to this view. 其中T级模型与此视图相关。

This approach allow me use model structure and map directly to my view. 这种方法允许我使用模型结构并直接映射到我的视图。 So any property defined in model has correspondent representation in my view. 因此,模型中定义的任何属性在我看来都有对应的表示。

So I can use next syntax to map my model to view: 所以我可以使用下一个语法来映射我的模型来查看:

for(let prop in this.model) {
   let ctrl = this.getControl(prop);
   ctrl.sendKeys(this.model[prop]);
}

So is not important how complex my form is - I need only define my model correct. 因此,我的形式是多么复杂并不重要 - 我只需要正确定义我的模型。

The problem I experiencing with checking data of the form as protractor returns promises when requesting page data: 我在检查表单数据作为量角器时遇到的问题在请求页面数据时返回promises:

for(let prop in this.model) {
  let ctrl = this.getControl(prop);
  expect(ctrl.getAttribute('value')).toEqual(this.model[prop] || '');
}

This code time to time throws: 这段代码不时抛出:

A Jasmine spec timed out. 茉莉花规格超时了。 Resetting the WebDriver Control Flow. 重置WebDriver控制流。

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调。

I has idea why this happens - because my loop can ends earlier that promise will resolve. 我知道为什么会发生这种情况 - 因为我的循环可以更早地结束承诺将解决。 I don't know how to deal with this situation. 我不知道如何处理这种情况。 I don't want to create promises chain or other Promise.all wrappers as Jasmine works good with promises so want to leave code clean here. 我不想创建promises链或其他Promise.all包装器,因为Jasmine适用于promises,所以想在这里保持代码清洁。

Maybe someone had this problems before and can share some ideas how to use Jasmine power here not switching to promise hell. 也许之前有人遇到过这个问题,并且可以分享一些如何使用Jasmine电源的想法,而不是转向承诺地狱。

You can pass a done callback to the it method in Jasmine, and in your test, once the async work is done, you can invoke this done function to signal to Jasmine that your async code has finished, eg: 您可以将done回调传递给Jasmine中的it方法,并且在测试中,一旦异步工作完成,您可以调用此done函数向Jasmine发出异步代码已完成的信号,例如:

it('does something', function(done) {
    someAsyncMethod().then(function() {
        expect(true).toEqual(true);
        done(); // signal to Jasmine async operation is complete
    }
});

See the Jasmine docs for more information 有关更多信息,请参阅Jasmine文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM