如何测试具有关系依赖关系的Ember模型的计算属性? [英] How to test an Ember model's computed property that has relations dependencies?

查看:77
本文介绍了如何测试具有关系依赖关系的Ember模型的计算属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Qunit测试来测试Ember模型,但是很难测试具有关系依赖关系的计算属性(计算属性触发另一个模型的计算属性)。

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).

正在测试的模型(CoffeeScript):

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')

会议模型:

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')

现在,测试 customerPhones firstCustomerFullName 确实让我很难受...

Now, testing customerPhones, and firstCustomerFullName is giving me a real hard time...

我的测试如下:

`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')

现在,我在此测试中使用了一些技术,这些技术是我在Stack Overflow的答案中找到的,但我似乎无法

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.

我该如何解决?

推荐答案

好的,到目前为止,我的评论太多了,所以我要做一个WIP答案。

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.

我将您的某些计算属性更改为 computed.alias 属性

I changed some of your computed properties to computed.alias properties

phone: (->
  @get('phones.firstObject.number')
).property('phones.firstObject.number')

phone: Ember.computed.alias('phones.firstObject.number')



  • 我撕了大部分设置后,Ember Data会急切地自行加载商店,并将使用灯具ID等而不指定它。 (这部分可以放回去,在这种情况下就没有必要了。)

  •   },(container, context) ->
      container.register 'store:main', DS.Store
      container.register 'adapter:application', DS.FixtureAdapter
      context.__setup_properties__.store = -> container.lookup('store:main')
    



    • 我事先表示歉意,我不是coffeescript的粉丝,所以我全都放在js中。现在的问题是,如果您仍然遇到任何问题,我们可能需要找出您使用的Ember,ED和Ember Qunit版本。

    • http://emberjs.jsbin.com/OxIDiVU/625/edit

      这篇关于如何测试具有关系依赖关系的Ember模型的计算属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆