Question

Following tutorials and other threads here I couldn't make tests run when having asynchronous code. Here's what I've been trying to test: http://emberjs.jsbin.com/xacurasi/1/edit?html,js,output with the tests shipped in the starter kit. When opening with ?test parameter The application window (EDIT: the frame which shows the actual application, not the qunit test page) shows a blank page.

Here's my only asynchronous code:

App.IndexRoute = Ember.Route.extend({
    model: function() {
        var promise;

        Ember.run(function() {
            promise = Em.$.getJSON('http://123.345.456.78/ajax/server.php');
        });

        return promise;
    }
});

Error I'm getting in the console:

Uncaught Error: 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

Which is quite confusing since I did wrap my async code in Ember.run(function() { //my code here});

Était-ce utile?

La solution

EDIT:

That error cleared things up. That error doesn't mean that the call to getJSON should be in an Ember.run, it means that the callback that getJSON calls should be in an Ember.run. It's a quirk of how Ember's run loop works. That code works fine in normal operations, but not in testing mode. There's a brief discussion about it here. What you'll need to do is provide a callback to the getJSON function. Something like this should fix it (and make it obvious as to what you're doing).

model: function() {
    return new Ember.RSVP.Promise(function(resolve) {
        Ember.$.getJSON('http://foobar', function(data) {
            Ember.run(null, resolve, data);
        });
    });
}

Or you could use the small library at the bottom of the thread that I linked to. But I figured I'd give you a solution using only Ember.js just in case.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top