jrabary
jrabary

Reputation: 2441

Headless testing Ember application with guard and jasmine

I'm trying to use jasmine and guard to test an ember based frontend for my rails 3.2 application. For this, I use jasminerice and guard-jasmine, phantomjs. The setup was very easy and simple and I can run some sample specs. When it comes to play with ember, things go mad. I have the spec below that test a video reader class. It should set videoHeight and videoWitdh property with the value of the corresponding video DOM element when calling loadVideo method. When I run jasmine on my browser the test pass, but when I run it with guard and phantomjs, it fails. I got "Expect undefined to equal 640". It seems that my DOM element is not taking into account when the test run on phantomjs.

describe "VideoReader", ->
  it "loads video from video DOM element", ->
    videoDOM = Ember.$("<video width='640' height='480'><source src='/test.mp4'></source></video>")[0];

    player = Topper.VideoReader.create();

    player.loadVideo(videoDOM);

    videoHeight = player.get('videoHeight');
    videoWidth  = player.get('videoWidth');

    expect(videoHeight).toEqual(480);
    expect(videoWidth).toEqual(640);

Is my spec correctly written ?

Upvotes: 2

Views: 3196

Answers (1)

pangratz
pangratz

Reputation: 16163

You should read the blog post about Testing in Ember.js.

I don't know what your Topper.VideoReader looks like but I guess your problem has to do with manipulating something on the DOM and not giving Ember.js a chance to propagate those changes. In your tests you should wrap your calls which interact with bindings or are manipulating the DOM in an Ember.run(function(){...}) call, as seen in the tests, for example child_views_test.js.


A summary from the discussion below:

  • If code you want to test interacts with the DOM, wrap it in an Ember.run call.
  • If you want to test for events which are fired in your code and you don't have control when exactly they are fired, use the asynchronous test features in the testing framework, like QUnit's asyncTest

Upvotes: 3

Related Questions