在测试中获取 Ember 属性将返回未定义



我是Ember的新手,我正在使用Konacha来测试我的应用程序。我在测试中使用数据存储时遇到问题。下面的代码尝试检查Item模型的 itemText 属性是否等于 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')没有做我所期望的。但是,在控制台中逐行运行相同的代码会为我返回所需的属性,因此我不确定哪里出错了。我有一种感觉,我可能以完全错误的方式对此进行了测试,因此任何反馈都值得赞赏。

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

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 

最新更新