4

I'm writing unit and integration tests against an Ember.js application, and I can't connect to the server when the application is under test. This means I need to use DS.FixtureAdapter to back up my data store.

But I'm not personally a fan of fixtures in large applications, because it's so hard to come up with a single set of fixtures that work with every test case. I prefer tools like factory_girl and machinist that allow me to generate test-specific data that's isolated from all other tests:

FactoryGirl.define do
  factory :user do
    name 'John Doe'
    date_of_birth { 21.years.ago }
  end
end

# In specific test cases:
user = FactoryGirl.build(:user)
young_user = FactoryGirl.create(:user, date_of_birth: 17.years.ago)

Of course, factory_girl and machinist can also generate related models automatically.

Is there any easy way to do this in Ember.js right now? Are there techniques, conventions or libraries which might make this easier? Googling does not real any options yet.

emk
  • 60,150
  • 6
  • 45
  • 50
  • hmmm those are cool. don't know what those two things are specifically but i can see that's ruby code. love that about emberjs...that it comes out of the ruby community and gets that kind of direction. – Randy L Apr 06 '13 at 23:33

3 Answers3

5

I created a project called Ember Data Factory Guy recently to help create fixture data for ember projects that use ember-data. It works with REST or ActiveModel adapter and has test helpers to make using it pretty easy.

Check it out here:

https://github.com/danielspaniel/ember-data-factory-guy

It supports belongsTo, hasMany ( even polymorphic ) associations .. sequences, embedded belongsTo .. and a few other things.

DanielSpaniel
  • 171
  • 1
  • 4
2

I have in the past used a rake task to create bunch of objects with FactoryGirl and then dump it out through the serializer into a fixtures.json file.

Pros:

  • It keeps things DRY when you have complex serializations logic or computed attributes.
  • You gain more confidence in your tests.

Cons:

  • It's a bit hackish.
  • Usability is so-so, because you have to remember to regenerate the fixtures file (slow, tedious).
  • It's hard to generate different sets of fixtures for different test cases, so you are mostly stuck with a global set of fixtures.

I don't have code around at the moment, but it's not a very complex setup. I'm on the fence as to whether the pros outweigh the cons.

By the way, generating fixtures on the fly for Konacha would be too slow to be usable, I've found (see #60).

Jo Liss
  • 30,333
  • 19
  • 121
  • 170
1

i think you can generate data in client and mock request. I suggest you can use jasmine or mocha with factory_girl js.

For generate data

FactoryGirl.define('user', function() {
      this.id = 1
})
FactoryGirl.create('user')

more details here: https://github.com/Coffa/factory_girl

emberjs

you can check here what's the setup to use Ember testing helpers?

Community
  • 1
  • 1
MQuy
  • 65
  • 1
  • 7