I'm writing Qunit tests to test An Ember model, but having a hard time testing computed properties that have a relation dependency (the computed property triggers another model's computed property).
The model that am testing (CoffeeScript):
Customer = DS.Model.extend
firstName: DS.attr('string')
lastName: DS.attr('string')
phones: DS.attr('embedded-list')
phone: (->
@get('phones.firstObject.number')
).property('phones.firstObject.number')
fullName: (->
[@get('lastName'), @get('firstName')].join(' ') )
).property('firstName','lastName')
The meeting Model:
Meeting = DS.Model.extend
customers: DS.hasMany('customer')
startAt: DS.attr('isodate')
status: DS.attr()
objective: DS.attr()
customerPhones: (->
phones = []
@get('customers').forEach (c) ->
c.get('phones').forEach (ph) ->
phones.push(ph.number)
phones
).property('customers.@each.phones')
firstCustomer: (->
@get('customers.firstObject')
).property('customers.firstObject')
firstCustomerFullName: (->
@get('firstCustomer.fullName')
).property('firstCustomer.fullName')
Now, testing customerPhones
, and firstCustomerFullName
is giving me a real hard time...
My test looks as follows:
`import { test, moduleForModel } from 'ember-qunit';`
moduleForModel('meeting', 'App.Meeting',{
needs: ['model:customer']
setup: ->
Ember.run do (t = @)->
->
customer = t.store().createRecord 'customer', firstName: 'John', lastName: 'Smith', phones:[]
customer.get('phones').addObject(Ember.Object.create({tag: 'home', number: '111222333'}))
customer.get('phones').addObject(Ember.Object.create({tag: 'work', number: '444555666'}))
t.subject().set('customers.content', Ember.ArrayProxy.create({content: []}));
t.subject().get('customers.content').pushObject(customer)
teardown: ->
},(container, context) ->
container.register 'store:main', DS.Store
container.register 'adapter:application', DS.FixtureAdapter
context.__setup_properties__.store = -> container.lookup('store:main')
)
test "it's a DS.Model", -> ok(@subject())
test "attributes: can be created with valid values", ->
meeting = @subject({objective: 'Follow up'})
Ember.run ->
equal(meeting.get('objective', 'Follow up'))
test "properties: firstCustomer & firstCustomerFullName & firstCustomerPhone", ->
meeting = @subject()
Ember.run ->
equal(meeting.get('firstCustomer.fullName'), 'Smith John')
equal(meeting.get('firstCustomer.phone'), '111222333')
Now, I used some techniques in this test, that I found in an answer here on Stack Overflow, but I can't seem to find it now.
That worked perfectly few days ago, now (it seems nonsense I know) whenever I run the test, it errors:
Assertion Failed: You cannot add 'meeting' records to this relationship (only 'meeting' allowed)
I don't know where the error is, nor how to fix it. Spent all the day monkeying around, No outcome.
How can I resolve this?
Okay, what I have so far is too much for a comment, so I'm going to do a WIP Answer.
I removed most of the run loops, they are only necessary for async processes.
I changed some of your computed properties to
computed.alias
propertiesi.e.
to
i.e.
http://emberjs.jsbin.com/OxIDiVU/625/edit
I found this question looking for 'how to unit test a computed property that uses a hasMany'.
Here is a simple example of how I did it (thanks to Kitler):
Fridge Model:
So say we now want to test inDateFoods in a unit test? Then do this in your fridge model test file:
They key here is to reopen your model to remove the has many, and push the object after doing
this.subject
. Before doing the reopen we were getting the errorAll elements of a hasMany relationship must be instances of DS.Model, you passed [[object Object]] error
.