简体   繁体   English

如何使用异步路由进行Ember单元测试?

[英]How to do Ember unit testing with asynchronous routes?

I'm using the functionality described in the Ember.js Asynchronous Routing guide. 我正在使用Ember.js异步路由指南中描述的功能。 Namely, I'm returning a promise from my asynchronous route's model hook to delay transitioning to the route, which works as expected. 也就是说,我从异步路由的model钩子返回一个promise,以延迟转换到路由,这可以按预期工作。

However, it breaks the ability to unit test my app. 但是,它打破了我的应用程序单元测试的能力。 When I run my tests, I get the following error in the console: 当我运行我的测试时,我在控制台中收到以下错误:

Assertion failed: You have turned on testing mode, which disabled the run-loop's autorun. 断言失败:您已打开测试模式,该模式禁用了运行循环的自动运行。 You will need to wrap any code with asynchronous side-effects in an Ember.run 您需要在Ember.run中包含任何带有异步副作用的代码

I've wrapped all code with asynchronous side-effects in Ember.run , but I still get the error. 我在Ember.run包装了所有带异步副作用的Ember.run ,但我仍然得到错误。

Here's a JSFiddle with a minimal example: http://jsfiddle.net/nRHfv/3/ 这是一个带有最小例子的JSFiddle: http//jsfiddle.net/nRHfv/3/

The example is based on the Ember Starter Kit and the test runner it comes with. 该示例基于Ember入门套件及其附带的测试运行器。 It has a working asynchronous index route. 它有一个工作的异步索引路由。 If you set testing: false to true in the _config object (line 10), it will turn on the test suite, and you should see the above error in your console. 如果在_config对象(第10行)中将testing: false设置为true ,它将打开测试套件,您应该在控制台中看到上述错误。

My asynchronous route's model hook is on line 38. I've tried several variations on wrapping the code in Ember.run . 我的异步路由的model钩子在第38行。我已经尝试了在Ember.run包装代码的几种变体。 For example, I've tried defining the promise outside of Ember.run , doing all the asynchronous stuff inside, and then returning the promise outside. 例如,我已经尝试在Ember.run之外定义promise,执行所有异步内容,然后在外面返回promise。 I've also tried wrapping just the contents of then() in Ember.run , like I've seen on some other answers (eg ember integration test error. dealing with asynchronous side-effects ). 我也尝试在Ember.run包装then()的内容,就像我在其他一些答案上看到的那样(例如, ember集成测试错误。处理异步副作用 )。

What am I doing wrong? 我究竟做错了什么?

After more searching, I found this notable comment on Guide: Asynchronous side-effects in testing on the Ember.js discussion forum by EmberSherpa : 更多的搜索后,我发现这个显着的评论指南:在测试异步副作用由Ember.js论坛EmberSherpa

BTW, there was a library released today that's designed to replace $.getJSON and makes this process a lot easier https://github.com/instructure/ic-ajax BTW,今天发布了一个库,旨在取代$ .getJSON并使这个过程变得更加容易https://github.com/instructure/ic-ajax

I tried the ic-ajax library, and it worked! 我尝试了ic-ajax库,它有效! After digging in to see what it's doing, here's how I should have written my asynchronous route's model hook: 在深入了解它正在做什么之后,我应该如何编写异步路由的model钩子:

var promise = new Ember.RSVP.Promise(function (resolve, reject) {
  Ember.$.ajax({
    url: '/echo/json/',
    type: 'POST',
    data: {
      json: '{ "text": "Hello from Ember.js" }',
      delay: 0.25
    },
    dataType: 'json',
    success: function (response, textStatus, jqXHR) {
      Ember.run(null, resolve, response);
    }
  })
});

return promise.then(function (response) {
  return response;
});

I guess jQuery promises are not interchangeable with Ember.RSVP.Promise when testing (but they are for triggering asynchronous routing actions). 我猜jQuery promises在测试时不能与Ember.RSVP.Promise互换(但它们用于触发异步路由操作)。

Here's an updated JSFiddle with asynchronous routing and testing working: http://jsfiddle.net/nRHfv/5/ 这是一个更新的JSFiddle,具有异步路由和测试工作: http//jsfiddle.net/nRHfv/5/

I think I'm going to stick with ic-ajax for the sake of cleaner code, and because of the ability to load static fixtures instead of live API endpoints when testing. 我想我会坚持使用ic-ajax为了更清晰的代码,并且因为在测试时能够加载静态灯具而不是实时API端点。

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

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