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.
First, a few notes that may help you clarify this and get the answer you are looking for.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With