(unit) testing in emberjs – munich ember.js meetup july 2014

30
(Unit) Testing in Stefan Fochler

Upload: istefo

Post on 20-Aug-2015

969 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

(Unit) Testingin

Stefan Fochler

Page 2: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014
Page 3: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

Page 4: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

• Make sure that stuff works…

Page 5: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

• Make sure that stuff works…• …even after code changes or refactoring

Page 6: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

• Make sure that stuff works…• …even after code changes or refactoring• Automation is key

Page 7: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

Page 8: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

• Unit Testing • Verify, that isolated chunks of functionality work • Don't worry about their dependencies

Page 9: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

• Unit Testing • Verify, that isolated chunks of functionality work • Don't worry about their dependencies

• Integration Testing • Test user interaction and application flow

Page 10: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

Page 11: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

About Testing

• Tools • QUnit testing framework • (async) helpers in ember-testing • Test Runner (testem) • Test Loader (ember-cli)

Page 12: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Short Demo

Page 13: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-QUnit

Page 14: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-QUnit• moduleFor(name, description, callbacks);

• Subject: controller:application, route:index etc.

Page 15: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-QUnit• moduleFor(name, description, callbacks);

• Subject: controller:application, route:index etc.• moduleForComponent(name, description, callbacks);

• Subject: x-foo, select-2 etc.

Page 16: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-QUnit• moduleFor(name, description, callbacks);

• Subject: controller:application, route:index etc.• moduleForComponent(name, description, callbacks);

• Subject: x-foo, select-2 etc.• moduleForModel(name, description, callbacks);

• Subject: user, table etc.

Page 17: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-QUnit• moduleFor(name, description, callbacks);

• Subject: controller:application, route:index etc.• moduleForComponent(name, description, callbacks);

• Subject: x-foo, select-2 etc.• moduleForModel(name, description, callbacks);

• Subject: user, table etc.• test(name, callback);

Page 18: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-Testing I

Page 19: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-Testing I• Async

• visit(url) • fillIn(selector, text) • click(selector) • keyEvent(selector, type, keyCode)

Page 20: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-Testing I• Async

• visit(url) • fillIn(selector, text) • click(selector) • keyEvent(selector, type, keyCode)

• Sync • find(selector, context) • currentPath() • currentURL()

Page 21: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-Testing II

Page 22: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-Testing II

• Wait • andThen(callback)

Page 23: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Helpers in Ember-Testing II

• Wait • andThen(callback)

• Custom Helpers • Ember.Test.registerHelper • Ember.Test.registerAsyncHelper

Page 24: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Testing Components Ivar App, component; !

moduleForComponent('select-2', 'Select2Component', { setup: function() { App = startApp(); // create instance of our component component = this.subject(); }, teardown: function() { Ember.run(App, 'destroy'); Ember.run(component, 'destroy'); } });

Page 25: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Testing Components IItest("it renders", function() { expect(2); !

equal(component.state, 'preRender'); !

// appends the component to the page this.append(); !

equal(component.state, 'inDOM'); });

Page 26: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Testing Components IIItest("it supports placeholder text", function() { var placeholder = "unit testing rocks"; !

component.set('placeholder', placeholder); !

this.append(); !

equal($('.select2-chosen').text(), placeholder, "has placeholder text"); });

Page 27: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Testing Components IVtest("it sets value to selected object", function() { expect(2); this.append(); component.set('content', simpleContent); !

// open options by clicking on the element click('.select2-choice'); // then select an option click('.select2-results li:nth-child(3)', 'body'); !

andThen(function() { equal(component.get(‘value’), simpleContent[2], "selects correct item"); equal($(‘.select2-chosen').text(), simpleContent[2].text, "has correct text"); }); });

Page 28: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Testing Components Vtest("it wraps the content in <pre><code>", function() { expect(2); // create wrapper view with template var component = Ember.View.extend({ template: Ember.Handlebars .compile('{{#highlight-code}}hello!{{/highlight-code}}') }).create(); !

Ember.run(function() { component.appendTo('#ember-testing'); }); !

ok(component.$("pre").length, "pre exists"); !

ok(component.$("pre > code").length, "pre > code exists"); });

Page 29: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Resources

• Ember Testing Guide http://emberjs.com/guides/testing/ • Ember-Cli Guide http://iamstef.net/ember-cli/ • Ember.js/Ember-Data tests

Page 30: (Unit) Testing in Emberjs – Munich Ember.js Meetup July 2014

Thanks!