2

私の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);
  })
)};

この計算されたプロパティは、モデル内のすべての要素をループし、プロパティのすべての値を追加してsubTotal、 を返しますcart total

カート-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');
});

TypeError: Cannot read property 'reduce' of null明らかにループするモデルがないため、テストは失敗します。

cartTotal計算されたプロパティの依存関係をモックして、テストに合格するにはどうすればよいですか?

ありがとう!

4

2 に答える 2