mercredi 8 juillet 2015

Unit test computed property on an Ember controller

The code from my controllers/cart.js:

export default Ember.Controller.extend({
  cartTotal: Ember.computed('model.@each.subTotal', function() {
    return this.model.reduce(function(subTotal, product) {
      var total = subTotal + product.get('subTotal');
      return total;
    }, 0);
  })
)};

This computed property loops over all the elements in the model, adding all the values of the subTotal property, returning a cart total.

cart-test.js

test('cartTotal function exists', function(assert) {
  var controller = this.subject();
  assert.equal(controller.get('cartTotal'), 30, 'The cart total function exists');
});

The test fails with TypeError: Cannot read property 'reduce' of null because it obviously doesn't have a model to loop over.

How can I mock the dependencies of the cartTotal computed property to make the test pass?

Thanks!

Aucun commentaire:

Enregistrer un commentaire