1

These are my first steps in testing in general so I don't know how to deal with it. On production I use REST adapter - Fixtures while developing.

For example having this User model:

Sks.User = DS.Model.extend
  name: DS.attr("string")
  email: DS.attr("string")

  gravatar: Ember.computed(->
    email = @get("email") || ""
    "http://www.gravatar.com/avatar/" + MD5(email)
  ).property("email")

  firstName: Ember.computed(->
    fName = @get('name').split(' ')[0]
  ).property("name")

  lastName: Ember.computed(->
    lName = @get('name').split(' ')[1]
  ).property("name")

how can I test it using TDD/BDD with Jasmine? I suppose that I should test the logic: first name, last name and gravatar but how can I translate it into Jasmine? I've read a lot about testing recently but theory you know is different than practice and I just hit the wall. :(

Should I simulate server to get a JSON or should I use fixtures?

wryrych
  • 1,755
  • 3
  • 19
  • 31

1 Answers1

0

I think for unit testing the models, you shouldn't deal with server (or even simulating the server) at all -- that is left up to the adapter. The unit tests for models would be for things like the computed properties (gravatar, firstname, lastname in this case) and other logic (life cycle callbacks, actions, etc). You can test those fairly straightforward -- they're mostly isolated from the server. Something like this:

describe "gravatar property", ->
  it "should append the md5 of the email to the gravatar path", ->
    user = Sks.User.createRecord name: "my name", email: "abc@def.com"
    expect(user.get('gravatar')).toBe "http://www.gravatar.com/avatar/#{MD5(user.get('email')}"

If you're using Ember-Data's RESTAdapter, you can assume that it's well tested (though you might want to write higher level integration tests to show that your models are getting loaded correctly with adapter data). If you're writing your own adapter, you should have a separate unit test for that.

What parts of the application to load

For unit tests, you really only need to load the main namespace (e.g. Sks = Ember.Application.create(...)) and the model definition (e.g. Sks.User = DS.Model.extend(...)) and its dependencies. Everything else can be stubbed by passing in mocks / stubs when you're creating your model (see: What is dependency injection?)

I've had some issues before with Ember's router autoloading in the test environment; you might want to disable the router (although this is only a problem if you're loading everything for your test -- I'm using jasminerice so all the javascript assets are compiled by the Rails server and the tests run in the browser) -- I think in the new Router you can use deferReadiness: http://emberjs.com/api/classes/Ember.Application.html#method_deferReadiness. Previously I was setting autoinit: false.

Community
  • 1
  • 1
Sherwin Yu
  • 2,940
  • 1
  • 21
  • 37
  • Do I have to load the full app in order to test a single model? Or can I load just a bare minimum (what should I load in case of Ember)? – wryrych Apr 21 '13 at 14:50
  • Check out my update -- for model unit tests, you basically only need to load the bare minimum (namespace + model + dependencies, but you should out dependencies for unit tests) – Sherwin Yu Apr 21 '13 at 19:24