在测试中获取Ember属性返回undefined

时间:2014-02-08 21:58:15

标签: testing ember.js ember-data

我是Ember的新手,我正在使用Konacha来测试我的应用。我在测试中使用数据存储时遇到问题。以下代码尝试检查itemText模型的Item属性是否等于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

显然,content = item.get('itemText')没有做我期望的事情。但是,在控制台中逐行运行相同的代码会让我回到我想要的属性,所以我不确定我哪里出错了。我有一种感觉,我可能会以完全错误的方式测试它,所以任何反馈都会受到赞赏。

1 个答案:

答案 0 :(得分:3)

我认为您的控制台日志是在获取模型之前运行的。你需要的是一个承诺,看看。

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