【问题标题】:Testing ember computed property with createRecord使用 createRecord 测试 ember 计算属性
【发布时间】:2014-08-30 15:10:41
【问题描述】:

我目前正在尝试在 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.[]'),
});

问题是,控制器上存在广告系列,但问题不是。关于为什么第二个断言没有通过的任何想法?

【问题讨论】:

    标签: ember.js qunit


    【解决方案1】:

    只是在这里吐口水,但你得到了问题,并在断言发生后推送该对象。

    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')

    【讨论】:

      猜你喜欢
      • 2014-10-31
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多