Sean
Sean

Reputation: 5820

Fetching Ember property in test returns undefined

I'm brand new to Ember and I'm using Konacha to test my app. I'm having trouble using the data store in my tests. The following code attempts to check if the itemText property of an Item model is equal to the displayed text in the DOM:

it "should display the item text", ->
  item = undefined
  Ember.run ->
    item = store.find('item', 1)  # store is defined in the spec helper
  content = item.get('itemText')
  console.log(item)               # Logs the correct item
  console.log(content)            # Logs undefined
  $('.list-item:eq(0)').text().should.equal content   # Fails since content is undefined

Clearly, the content = item.get('itemText') is not doing what I'm expecting it to. However, running the same code line by line in the console gives me back the property I want, so I'm not sure where I'm going wrong. I have a feeling I may be testing this in entirely the wrong way, so any feedback is appreciated.

Upvotes: 1

Views: 176

Answers (1)

MartinElvar
MartinElvar

Reputation: 5804

I think your console logs is ran before the the model is fetched. What you need is a promise, have a look.

it "should display the item text", ->
  item = undefined
  Ember.run ->
    item = store.find('item', 1).then (item) ->
      content = item.get('itemText')
       $('.list-item:eq(0)').text().should.equal content 

Upvotes: 3

Related Questions