wryrych
wryrych

Reputation: 1765

what's the setup to use Ember testing helpers?

How do you setup your tests in order to use new Ember testing helpers like visit or find. I have some difficulty to make it work.

I tried the following:

before creating namespace: Ember.testing = true

in my spec:

App.setupForTesting()

App.injectTestHelpers()

Then I get a message that says that I will need to wrap any asynchronous code with Ember.run so I did, but my fixture data (mocking GET requests) is somehow inaccessible so I can't set data to them.

Have you figured it out?

Upvotes: 2

Views: 1839

Answers (1)

Toran Billups
Toran Billups

Reputation: 27399

Assuming you are using RC5 (not RC6 just yet because it has a slight bug)

document.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');

Ember.testing = true;

App.rootElement = '#ember-testing';
App.setupForTesting();
App.injectTestHelpers();

function exists(selector) {
    return !!find(selector).length;
}

function stubEndpointForHttpRequest(url, json) {
    $.mockjax({
        url: url,
        dataType: 'json',
        responseText: json
    });
}

$.mockjaxSettings.logging = false;
$.mockjaxSettings.responseTime = 0;

Then you would write your test like so

module('integration tests', {
    setup: function() {
        App.reset();
        App.Person.people = [];
    },
    teardown: function() {
        $.mockjaxClear();
    }
});

test('ajax response with 2 people yields table with 2 rows', function() {
    var json = [{firstName: "x", lastName: "y"}, {firstName: "h", lastName: "z"}];
    stubEndpointForHttpRequest('/api/people', json);
    visit("/").then(function() {
        var rows = find("table tr").length;
        equal(rows, 2, rows);
    });
});

Here is a full blown example project showing how to add the integration helper shown above and a few basic integration tests (using karma / qunit / jquery-mockjax)

https://github.com/toranb/ember-testing-example

Upvotes: 4

Related Questions