Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js/Rails integration testing with fixtures

I am trying to set up a testing environment for my ember.js rails application, and have decided to use Konacha with mocha and chai. The largest problem I have at the moment is setting up fixtures for my ember models in the testing environment. Would any of you be able to explain your file structure and setup if you have this implemented? There are a few sites out there that explain it very briefly, but I would prefer a clearer explanation.

like image 749
Bobby Hazel Avatar asked Oct 22 '13 14:10

Bobby Hazel


1 Answers

First, a few notes that may help you clarify this and get the answer you are looking for.

  1. Rails fixtures and Ember.js fixtures aren't related at all, Rails has little to do with it.
  2. Ember.js fixtures can be declared in any file that your test suite requires.
  3. Ember.js fixtures are permanent and can not be torn down. That means they keep state between your tests.
  4. ember-data will persist records between tests if you don't explicitly destroy them.

With all that said, here is a former test_helper.coffee file from one of my projects. The file will configure Ember for testing and pre-load your fixtures. The project was using mocha and chai for testing—no other libraries are necessary. Hopefully the CoffeeScript doesn't cause an issue:

#= require_tree .

Efflux.setupForTesting()
Efflux.injectTestHelpers()

Ember.Test.adapter = Ember.Test.Adapter.extend
  exception: (error) ->
    Ember.inspect(error)
    throw error

Efflux.Store = DS.Store.extend
  adapter: DS.FixtureAdapter.create(simulateRemoteResponse: false)
  revision: 13

Efflux.Tag.FIXTURES = [
  { id: 1
    name: 'alpha'
    group: 'symbols'
  },
  { id: 2
    name: 'beta'
    group: 'symbols'
  },
  { id: 3
    name: 'gamma'
    group: 'symbols'
  }
]

Here is an example test written in the BDD style and using the Tag.FIXTURES:

describe '#alphaSort', ->
  it 'combines the group and name into a single property', ->
    Ember.run ->
      tag = Efflux.Tag.find(3)
      tag.one 'didLoad', ->
        tag.set('group', 'greek')
        tag.set('name', 'alpha')

        expect(tag.get('alphaSort')).to.eq('greekalpha')

The important things to note there are that Ember.run is being used for the entirety of the test, and the data isn't available until after the didLoad event is triggered. Some of the data hooks may have changed since this was written, but that is the general idea.

like image 72
Parker Selbert Avatar answered Nov 03 '22 03:11

Parker Selbert