abuiles / ember-cli-101-errata

18 stars 4 forks source link

Fix for test/unit/components/articles/article-row-test.js #218

Open pablobm opened 9 years ago

pablobm commented 9 years ago

The generated test for the component articles/article-row fails with the message "article is null". This is not strictly an error in the book, but it can confuse some people, and existing online documentation doesn't seem to cover this well.

There are actually two problems. First, the component requires an article instance to work, but the test initializes the component without providing an article. A simple fix is to provide a mock article. For example:

var article = Ember.Object.create();
var component = this.subject({article});

This will fix the initial problem, but a new one will arise: the component requires the formatted-date helper to work. Therefore this has to be specified in the needs argument to module For Component:

needs: ['helper:formatted-date'],

The following is a complete, working example:

import Ember from 'ember';
import { moduleForComponent, test } from 'ember-qunit';

moduleForComponent('articles/article-row', 'Unit | Component | articles/article row', {
  needs: ['helper:formatted-date'],
  unit: true
});

test('it renders', function(assert) {
  assert.expect(2);

  var article = Ember.Object.create();

  // Creates the component instance
  var component = this.subject({article});
  assert.equal(component._state, 'preRender');

  // Renders the component to the page
  this.render();
  assert.equal(component._state, 'inDOM');
});