3

i'm unit testing computed properties on a DS.Model .

// a trivial example that i'm not actually testing
App.ModelA = DS.Model.extend({
    sub_models : DS.hasMany('App.ModelB'),

    lengthOfBees : function() {
         return this.sub_models.length;
    }.property('sub_models.length')
})

how do i manually create a record that has an association? providing json for a record with no associations is easy.. App.ModelX = App.ModelX.createRecord({ attribA : 'valueA'});

but i don't know the syntax for createRecord when i want to supply the hasMany associations. how do i do that?

4

1 回答 1

3

假设您使用的是 ember-data rev 12,这应该可以

  test("findMany QUnit test example", function() {
    store.load(Person, {id: 9, name: "Toran Billups"});
    person = store.find(Person, 9);
    store.loadHasMany(person, 'tasks', [ 1, 2 ]);

    var tasks = get(person, 'tasks'); //to fire the http request

    equal(get(tasks, 'length'), 2, "");
  });

如果 loadHasMany 在您的 ember-data 版本中不起作用,请尝试使用此方法

  test("findMany QUnit test example", function() {
    store.load(Person, {id: 9, name: "Toran Billups", tasks: [1, 2]});
    person = store.find(Person, 9);

    var tasks = get(person, 'tasks'); //to fire the http request

    equal(get(tasks, 'length'), 2, "");
  });
于 2013-05-09T15:15:27.413 回答