$30 off During Our Annual Pro Sale. View Details »

testing-ember-apps.pdf

 testing-ember-apps.pdf

Tom Kruijsen

March 12, 2014
Tweet

More Decks by Tom Kruijsen

Other Decks in Programming

Transcript

  1. Testing Ember Apps
    Tom Kruijsen
    Ember.js - 2014-03-12

    View Slide

  2. Brightin

    View Slide

  3. View Slide

  4. Warning: blatant self-
    promotion

    View Slide

  5. Appuccino

    View Slide

  6. Some questions

    View Slide

  7. Integration tests with
    QUnit

    View Slide

  8. Unit tests?

    View Slide

  9. Why QUnit?

    View Slide

  10. View Slide

  11. Konacha

    View Slide

  12. Rails gem

    View Slide

  13. Mocha & Chai

    View Slide

  14. View Slide

  15. #= require 'sinon'!
    #= require 'application'!
    !
    mocha.ui('bdd')!
    mocha.globals(['Ember', 'DS', 'Catstagram', 'MD5'])!
    mocha.timeout(5)!
    chai.Assertion.includeStack = true!
    !
    ENV = {!
    TESTING: true!
    }!
    !
    window.server = sinon.fakeServer.create()!
    !
    window.testHelper =!
    lookup: (object, object_name) ->!
    name = object_name || "main"!
    Catstagram.__container__.lookup(object + ":" + name)!
    !
    Catstagram.Router.reopen!
    location: 'none'!
    !
    Konacha.reset = Ember.K!
    !
    Catstagram.setupForTesting()!
    Catstagram.injectTestHelpers()!
    !
    beforeEach ->!
    Ember.run ->!
    Catstagram.reset()!
    !
    Ember.Test.adapter = Ember.Test.Adapter.create()

    View Slide

  16. #= require spec_helper!
    !
    describe "Cat", ->!
    it "is a DS.Model", ->!
    expect(Catstagram.Cat).to.exist!
    expect(DS.Model.detect(Catstagram.Cat)).to.exist!
    !
    it "has a name", =>!
    Ember.run( ->!
    cat = testHelper.lookup('store').createRecord 'cat',
    name: 'Mimi'!
    expect(cat.get 'name').to.equal 'Mimi'!
    )

    View Slide

  17. #= require 'spec_helper'!
    !
    describe 'Cats page', ->!
    beforeEach ->!
    cat = Ember.Object.create!
    id: 1!
    name: 'Mimi'!
    !
    stubStore = sinon.stub(testHelper.lookup('store'), 'find')!
    stubStore.withArgs('cat').returns([cat])!
    !
    !
    visit '/'!
    !
    it 'shows the names of the cats', ->!
    expect($('li').text()).to.equal('Mimi')!
    !
    it 'links to a cat page', ->!
    mock = sinon.mock(testHelper.lookup('route', 'cats_cat'))!
    mock.expects('setupController').once()!
    $('a').click()!
    mock.verify()

    View Slide

  18. Mocha is nicer than
    QUnit

    View Slide

  19. Use Sinon and mock a
    lot

    View Slide

  20. How do you
    test your apps?

    View Slide

  21. Testing Ember Apps
    Tom Kruijsen
    Ember.js - 2014-03-12

    View Slide