50
WELCOME, EMBEERISTS to the Ember.js Meetup #3 in Brussels 15th of May 2014

Ember.js Brussels Meetup #3 - Testing your Ember.js app

  • Upload
    yoranbe

  • View
    1.730

  • Download
    4

Embed Size (px)

DESCRIPTION

Presentation of the third Brussels Ember.js meetup (http://www.meetup.com/Ember-js-Brussels/). Contains - Integration tests for your Ember.js application - How to call A from B in Ember.js

Citation preview

Page 1: Ember.js Brussels Meetup #3 - Testing your Ember.js app

WELCOME, EMBEERISTSto the Ember.js Meetup #3 in Brussels

15th of May 2014

Page 2: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Thank you for the venueBetaGroup

is our beer provider tonightHstry

Page 3: Ember.js Brussels Meetup #3 - Testing your Ember.js app

SCHEDULEHow to test your Ember app (Yoran)

Boost the loading time of your Ember app with data pre-loading (Boris)

How to call A from B in Ember (Yoran)

Page 4: Ember.js Brussels Meetup #3 - Testing your Ember.js app

ABOUT MEWorks at (hiring Ember devs!)Hstry

Blogs on YoranBrondsema.com

Tweets on @YoranBrondsema

Page 5: Ember.js Brussels Meetup #3 - Testing your Ember.js app

ABOUT THIS MEETUPLooking for organizer(s)!

Page 6: Ember.js Brussels Meetup #3 - Testing your Ember.js app

TESTING AN EMBER.JS APPFrom my experiences working on Hstry

Ember.js front-end and Ruby on Rails back-end

Will cover the whole stack

Page 7: Ember.js Brussels Meetup #3 - Testing your Ember.js app

I hope you do tests. And if youdon't, you should.

Page 8: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Prevent regressions

Page 9: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Confidence

Page 10: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Enables methodologies like BDD and TDD

Page 11: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Documentation

Page 12: Ember.js Brussels Meetup #3 - Testing your Ember.js app

3 levels of testing

Page 13: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Unit tests

Page 14: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Controller tests

Page 15: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Integration tests

Page 16: Ember.js Brussels Meetup #3 - Testing your Ember.js app

UNIT TESTSBelong to back-end

Test your models

Validations on your data

Instance methods

Class methods

Page 17: Ember.js Brussels Meetup #3 - Testing your Ember.js app

describe BlogPost do let(:blog_post) { FactoryGirl.build(:blog_post) }

it("has a title") { expect(blog_post).to validate_presence_of(:title) }

describe "#related_posts" do let(:related_posts) { blog_post.related_posts } it("returns an array of at most 5 posts") { ... } end end

Page 18: Ember.js Brussels Meetup #3 - Testing your Ember.js app

CONTROLLER TESTSBelong to back-end

Interface between front-end and back-end

More important than unit tests

Test error codes

Test changes to models

Test structure of response

Page 19: Ember.js Brussels Meetup #3 - Testing your Ember.js app

1: parametervalidation

2: updatemodels

3: sendresponse

POST /blog_posts

401 (unauthorized)403 (forbidden)

412 (precondition failed)

422 (unprocessable entity)

{blog_post: {id: 1,title: “Ember.js meetup”,content: “...”}}

Page 20: Ember.js Brussels Meetup #3 - Testing your Ember.js app

describe BlogPostsController do describe "POST #create" do # PARAMETER VALIDATION context "Invalid authentication token" do it("returns 'unauthorized' status code") { ... } end context "Blank title" do it("returns 'unprocessable_entity' status code") { ... } end

context "Valid parameters" do # MODELS UPDATE it("creates a new BlogPost") { ... }

# SEND RESPONSE it("returns '201 Created' status code") { ... } it("returns a JSON serialization of a blog post") { ... } end endend

Page 21: Ember.js Brussels Meetup #3 - Testing your Ember.js app

INTEGRATION TESTSSimulates user behavior in browser

Back in "old" days, was driven by back-end (Rails views)

In Rails, tested with RSpec + Capybara

Run on Selenium, PhantomJS, ...

Page 22: Ember.js Brussels Meetup #3 - Testing your Ember.js app

This is how the application was tested too

Hstry

Page 23: Ember.js Brussels Meetup #3 - Testing your Ember.js app

... but it had problems!

Page 24: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Very slow

Starts a new fresh browser session for each test

Page 25: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Synchronization issues

Move on to next test while controller actions are still running. Test failure.

it “shows the title” BrowserGET /blog_post/1

GET /blog_post/1/commentsit “shows the content”

Page 26: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Random fails

Page 27: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Do all integration testing inEmber.js itself

Page 28: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Way faster

Moved from ~25 minutes to ~2 minutes testing time

Page 29: Ember.js Brussels Meetup #3 - Testing your Ember.js app

No more synchronization issues

Promises!

Page 30: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Promotes separation between back-end and front-end

Page 31: Ember.js Brussels Meetup #3 - Testing your Ember.js app

What to test?

Page 32: Ember.js Brussels Meetup #3 - Testing your Ember.js app

What the user is supposed to see.

Click on this button, should see this.

Page 33: Ember.js Brussels Meetup #3 - Testing your Ember.js app

That calls to API with proper parameters are made.

Click on this "Submit" button, issues POST request to endpoint XXX with parametersYYY

We know that if parameters are ok, it does what it needs to do

Page 34: Ember.js Brussels Meetup #3 - Testing your Ember.js app

What do you need?

Testing framework: QUnit (default for Ember), Mocha,Jasmine,...

Test runner: Teaspoon (Rails), Testem (Node.js), Karma(Node.js)

Page 35: Ember.js Brussels Meetup #3 - Testing your Ember.js app

DEMO

Page 37: Ember.js Brussels Meetup #3 - Testing your Ember.js app

What about seed data?Set up seed data in API endpoint /test_reset

Call this endpoint before each test

Do not open this endpoint in production!

Page 38: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Testing Ember components

Isolated components with a clear interface, ideal for testing

"Test once, re-use with confidence everywhere"

See Ember.js Guide: Testing Components

Page 39: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Short intro to integration testing in Ember.js

Questions?

Page 40: Ember.js Brussels Meetup #3 - Testing your Ember.js app

THANK YOUOh, and Hstry is still hiring :-)

Page 41: Ember.js Brussels Meetup #3 - Testing your Ember.js app

HOW TO CALL A FROM B INEMBER.JS

* from talk at Boston Ember.js meetup by Ben Donaldson

Page 42: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Ember.js has many layers

Routes

Controllers

Views

Templates

Page 43: Ember.js Brussels Meetup #3 - Testing your Ember.js app

How do you call layer X whenyou're in layer Y?

Page 44: Ember.js Brussels Meetup #3 - Testing your Ember.js app

We have two models Apple and Orange

Each has its own route, controller, view and template

Page 45: Ember.js Brussels Meetup #3 - Testing your Ember.js app

I'm in AppleRoute// Applethis.modelFor('apple');

// OrangeRoutethis.get('router').getHandler('orange');

// AppleControllerthis.controllerFor('apple');

// AppleView???

// AppleTemplate???

Page 46: Ember.js Brussels Meetup #3 - Testing your Ember.js app

I'm in AppleController// Applethis.get('content');this.get('model');

// AppleRoutethis.get('target');

// OrangeControllerApp.AppleController = Ember.Controller.extend({ ..., needs: ['orange'], ...});this.get('controllers.orange');

// AppleViewApp.AppleView = Ember.View.extend({ _setConnection: function() { this.get('controller').on('event', this, this.eventHappened); }.on('init'), eventHappened: function() { ... }});this.trigger('event'); // in AppleController

// AppleTemplate???

Page 47: Ember.js Brussels Meetup #3 - Testing your Ember.js app

I'm in AppleView// Applethis.get('context');this.get('controller.content');this.get('controller.model');

// AppleRoutethis.get('controller.target');

// AppleControllerthis.get('controller');

// OrangeView???

// AppleTemplatethis.get('template'); // Handlebars functionthis.get('element'); // DOM element

Page 48: Ember.js Brussels Meetup #3 - Testing your Ember.js app

I'm in AppleTemplate// Apple{{view.context}}{{controller.content}}{{controller.model}}

// AppleRoute{{controller.target}}

// AppleController{{controller}}

// AppleView{{view}}

// OrangeTemplate???

Page 49: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Ugly, but if necessary...

// Via the container (accessible pretty much everywhere)this.get('container').lookup('route:apple');// WorseApp.__container__.lookup('route:apple');

// Manual connections on 'init'App.AppleView = Ember.View.extend({ _setConnections: function() { this.get('controller').set('appleView', this); }.on('init')});

Page 50: Ember.js Brussels Meetup #3 - Testing your Ember.js app

Questions?