Random thoughts about the world of programming

Testing Ember.js - Part 1

Feb 19th, 2013

Ever since I saw the testing slides from EmberCamp I was thinking
about testing. Up until now I’ve been using Capybara which is really
really really slow.

But @joliss mentioned this thing called Ember.testing which should
automagically fix all of the async problems which make tests ugly, such
as waiting for the application to initialize and finish routing.

In its essence Ember.testing = true disables the automatic runloop,
which gives you the control to manually schedule asynchronous operations
to happen in a one-off runloop via Ember.run.

Ember.run will run the given function inside a runloop and flush all
of the bindings before it finishes, which means you can render a view
inside Ember.run and check the DOM right after that. Here’s an example
from the Ember.View tests

// Testing mode disables automatic runloopEmber.testing=true;// Creating an application normally happens async,// which is why we have to wrap it in Ember.runEmber.run(function(){App=Ember.Application.create();});App.Router.map(function(){this.route("home",{path:"/"});});App.Store=DS.Store.extend({revision:11,adapter:DS.FixtureAdapter.extend({// This will make the FixtureAdapter do everything synchronously// instead of using setTimeout, which is vital because setTimeout// happens outside of the runloop.simulateRemoteResponse:false})});App.User=DS.Model.extend({name:DS.attr("string")});App.User.FIXTURES=[{id:1,name:"brohuda"}];App.HomeRoute=Ember.Route.extend({model:function(){returnApp.User.find(1);}});// Enabling Ember.testing will also disable automatic initialization,// which forces us to initialize manuallyEmber.run(function(){App.initialize();});// In real life this would be an assertion,// here we'll just check if everything is rendered at this point in time.$("p strong").append($("h2").text());

Take the example apart, play with it and try to figure out what works
and what doesn’t :)

it means that you forgot to wrap something in Ember.run. I hope this
is a good enough introduction. In one of the upcoming articles we’ll
take a look at simple Ember application and try testing it with a
full featured testing framework.