本文介绍了在余烬气候中引入瞬时延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ember-cli-mirage进行验收测试。对于特定情况,我想在通过慢速连接获取数据时检查行为。

I am using ember-cli-mirage for acceptance tests. For a specific case, I would like to check the behaviour while fetching data over a slow connection.

ember-cli-mirage中有一个设置称为 timing 模拟响应中的延迟。但是,不能在特定测试中将此设置更改为其他值:

There's a setting in ember-cli-mirage called timing that simulates a delay in the response. However, this setting cannot be changed to be different in a specific test:

// app/mirage/config.js
this.timing = 400;

我尝试过的其他方法是在虚假端点处返回承诺。通过一些导入/导出,我可以从测试中控制承诺的解决方案。不幸的是,ember-cli-mirage似乎没有将返回值识别为承诺,而只是将其逐字传递回适配器:

Something else I have tried is returning a promise at the fake endpoint. Through some import/export, I could control the resolution of the promise from my test. Unfortunately, ember-cli-mirage doesn't seem to recognise the return value as a promise, and simply passes it back to the adapter verbatim:

// app/mirage/config.js
this.get('/StopPoint/Search/:term', (db, request) => {
  return freezer.run(function() {
    return db[`stop-point-search-${request.params.term}`][0];
  });
});

// At my test
freezer.on()
runTests()
freezer.off()

问题:有什么办法可以做到这一点?即:要控制ember-cli-mirage中特定响应的延迟?

The question: is there any way to do this? Ie: to control the delay of a specific response in ember-cli-mirage?

推荐答案

一些想法:


  • 您可以通过 server.timing 在特定测试中更改计时。应该为每个测试重新初始化服务器,这样才不会影响其他测试。

  • You can change timing within a specific test via server.timing. The server should be reinstantiated for each test so this wouldn't affect other tests.

test('for slow behavior', function() {
  server.timing = 400;
  //
});


  • 您还可以在测试中重新定义路由处理程序,如。如果您使用的是 0.2.0-beta ,则路由处理程序具有 timing 选项,您可以使用它来影响处理程序:

  • You can also redefine route handlers within tests as shown here in the second example of the Acceptance testing guides. If you're using 0.2.0-beta, route handlers have a timing option you can use to affect just that handler:

    test('for slow behavior', function() {
      server.get('/slow-query', (schema, request) => {
        //
        return data;
      }, {timing: 400};
    
      visit('/');
      //
      assert();
    });
    


  • 我想您的直觉是返回可以控制冻结的东西,这是测试它的理想方式,与。也许Mirage最终可以为此添加API。

    I think your instinct to return something you have control over freezing would be the ideal way to test this, in tandem with something like Timecop. Perhaps Mirage can add an API for this eventually.

    这篇关于在余烬气候中引入瞬时延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-18 23:39