EmberScreencasts

Custom Test Helpers

in ember-cli

Testing can get repetitive

ember g test-helper sign-in
test("I can program a computer", function(){
    visit('/login');
    andThen(function(){
      fillIn('#email-address', 'tony@starkindustries.com');
      fillIn('#password', 'i<3ultron');
      click('#log-in');
    });
    andThen(function(){
      visit('/programming');
    });
    andThen(function(){
      equal($('#jarvis').text(), 'Greetings, Mr. Stark');
    });
});
test("I can fly an iron suit", function(){
    visit('/login');
    andThen(function(){
      fillIn('#email-address', 'tony@starkindustries.com');
      fillIn('#password', 'i<3ultron');
      click('#log-in');
    });
    andThen(function(){
      click('#suit-up');
    });
    andThen(function(){
      equal($('#iron-man').text(), 'Stark');
    });
});

Custom Helpers -> DRY

ember g test-helper sign-in
test("I can program a computer", function(){
    signIn();
    andThen(function(){
      visit('/programming');
    });
    andThen(function(){
      equal($('#jarvis').text(), 'Greetings, Mr. Stark');
    });
});
test("I can fly an iron suit", function(){
    signIn();
    andThen(function(){
      click('#suit-up');
    });
    andThen(function(){
      equal($('#iron-man').text(), 'Stark');
    });
});

Generate your Helper

import Ember from 'ember';

export default Ember.Test.registerAsyncHelper('signIn', 
    function(app) {
    }
);
ember g test-helper sign-in

Write your helper

import Ember from 'ember';

export default Ember.Test.registerAsyncHelper('signIn', 
  function(app) {
    visit('/login');
    andThen(function(){
      fillIn('#email-address', 'tony@starkindustries.com');
      fillIn('#password', 'i<3ultron');
      click('#log-in');
    });
  }
);

Arguments

import Ember from 'ember';

export default Ember.Test.registerAsyncHelper('signIn', 
  function(app, username, password) {
    visit('/login');
    andThen(function(){
      fillIn('#email-address', username);
      fillIn('#password', password);
      click('#log-in');
    });
  }
);
signIn('tony@starkindustries.com', 'i<3ultron')

Ember-CLI Shotgun Boilerplate

//start-app.js
import signIn from './sign-in';

//tests/.jshintrc
{
  predef: [
    'signIn',
    ...
  ]
}

Writing a Custom Test Helper in Ember-CLI

  1. Recognize repetition or something repeating
  2. `ember g test-helper helper-name`
  3. Write your helper
  4. Put in start-app.js and .jshintrc boilerplate
Made with Slides.com