1
votes

I'm currently trying to unit test a computed property on a controller in ember. I have a campaign model on the controller, and there are many questions to the campaign. The computed property checks whether or not there are questions on the campaign. Code for the test and the controller are below:

Test:

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

Controller and Property:

App.CampaignDeploymentController = Em.ObjectController.extend({
  questionsLength: function() {
    return this.get('content.questions.length');
  }.property('content.questions.[]'),
});

The issue is, the campaign is present on the controller but the question is not. Any ideas as to why the second assertion isn't passing?

1

1 Answers

0
votes

Just spit-balling here, but you're getting the questions, and pushing that object after the assert has happened.

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

Also your count could be simplified, for kicks and giggles, so essentially you could just use questions.length everywhere as well, but no biggie.

questionsLength: Ember.computed.alias('questions.length')