我正在尝试在ember中对控制器上的计算属性进行单元测试。我在控制器上有一个广告系列模型,广告系列有很多问题。计算属性检查是否有关于广告系列的问题。测试代码和控制器如下:
测试:
test("questionLength property", function() {
expect(2);
var controller = App.__container__.lookup('controller:campaignDeployment');
var store = controller.get('store');
var campaign = store.createRecord('campaign', {id: 500});
Ember.run(function() {
controller.set('model', campaign);
equal(controller.get('questionsLength'), 0, 'should not have question length');
campaign.get('questions').then(function() {
campaign.get('questions').pushObject(store.createRecord('question', {
id: 678,
text: 'Test Question',
link: 'http://www.jebbit.com'
}));
});
controller.set('model', campaign);
equal(controller.get('questionsLength'), 1, 'should have question length');
});
});
控制器和财产:
App.CampaignDeploymentController = Em.ObjectController.extend({
questionsLength: function() {
return this.get('content.questions.length');
}.property('content.questions.[]'),
});
问题是,广告系列出现在控制器上,但问题并非如此。关于为什么第二个断言没有通过的任何想法?
答案 0 :(得分:0)
这里只是吐痰,但你得到问题,并在断言发生后推动该对象。
test("questionLength property", function() {
expect(2);
var controller = App.__container__.lookup('controller:campaignDeployment');
var store = controller.get('store');
var campaign = store.createRecord('campaign', {id: 500});
Ember.run(function() {
controller.set('model', campaign);
equal(controller.get('questionsLength'), 0, 'should not have question length');
stop();
campaign.get('questions').then(function(questions) {
start()
questions.pushObject(store.createRecord('question', {
id: 678,
text: 'Test Question',
link: 'http://www.jebbit.com'
}));
equal(controller.get('questionsLength'), 1, 'should have question length');
});
});
});
此外,你的计数可以简化,因为踢腿和咯咯笑,所以基本上你也可以在任何地方使用questions.length
,但没有大事。
questionsLength:Ember.computed.alias(' questions.length')