我的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
计算属性的依赖关系以使测试通过?
谢谢!