简体   繁体   中英

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

import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';

moduleFor('controller:cart', {
  // Specify the other units that are required for this test.
  // needs: ['controller:foo']
});

test('it exists', function(assert) {
  var controller = this.subject();
  assert.ok(controller);
});

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!

Something along these lines maybe?

import { moduleFor, test } from 'ember-qunit';

import Ember from 'ember';

var products = [
  Ember.Object.create({ name: 'shoe', subTotal: 10 }), 
  Ember.Object.create({ name: 'shirt', subTotal: 20 })];

var model = Ember.ArrayProxy.create({
  content: Ember.A(products)
});

moduleFor('controller:cart', {
  beforeEach() {
    this.controller = this.subject();
  }
});

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

One way to deal with this would be to stub the model in the beforeEach hook:

var sampleModel = [ // sample data that follows your actual model structure ]

moduleFor('controller:cart', {
  beforeEach() {
    this.controller = this.subject(); // allows you to access it in the tests without having to redefine it each time
    this.controller.set('model', sampleModel);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM