Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Full Stack Integration Testing with Node.js

Swift
September 17, 2012

Full Stack Integration Testing with Node.js

These are the slides from my talk at NYC Node. The presentation is an overview of full-stack integration testing and how it can be more efficient and faster with Node.js

Swift

September 17, 2012
Tweet

More Decks by Swift

Other Decks in Technology

Transcript

  1. @SwiftAlphaOne it("should list all persons in response", function() { var

    response = readFixtures("persons.json"); var options = {}; var view = new PersonsView({ collection: new Persons() }); view.render(); fakeResponse(response, options, function() { view.collection.fetch(); }); expect(view.$('.persons li').length).toEqual(20); }); Exhibit A
  2. @SwiftAlphaOne $(function() { /* ... */ var AppView = Backbone.View.extend({

    el: $("#myapp"), initialize: function() { var view = new PersonsView({ collection: new Persons() }); view.collection.fetch(); }, /* ... */ }); var App = new AppView; }); WHAT IF I NEVER CALL RENDER?
  3. @SwiftAlphaOne A BETTER STRATEGY PHASE 1: PHASE 2: PHASE 3:

    Load the page in a browser* Perform the necessary actions Profit. *simulated browsers are ok too (And verify that they behave as expected)
  4. @SwiftAlphaOne var browser = new Browser() , fakeData = readFixtures("persons.json");

    fakeAPIResponse('/persons', fakeData); browser.visit("http://localhost:3000/", function () { assert.ok(browser.success); var persons = browser.queryAll(".persons li"); assert.lengthOf(persons, 20); }); Exhibit B
  5. @SwiftAlphaOne Feature: viewing a list of persons In order to

    figure out who is awesome As a visitor I want to see a list of awesome persons Scenario: viewing a list of persons Given "/api/v1/users" returns the following JSON response: """ [{"name": "swift"}, {"name": "john"}] """ When I visit the home page Then I should see 2 persons Exhibit C
  6. @SwiftAlphaOne sharedSteps = module.exports = -> @World = require("../support/world").World @When

    /^I visit the home page$/, (next) -> @visit "/", next @Then /^I should see (\d+) persons$/, (count, next) -> persons = @browser.queryAll(".persons li") assert.lengthOf(persons, parseInt(count)) next() UNDER THE HOOD
  7. @SwiftAlphaOne var app = require('../../app') , Browser = require('zombie'); Browser.site

    = 'http://localhost:3000'; function World(callback) { this.browser = new Browser(); this.visit = function(url, callback) { this.browser.visit(url, callback); }; callback(); }; exports.World = World; UNDER THE HOOD (continued...)
  8. @SwiftAlphaOne fakeAPIResponse = (url, response, next) -> @mock.get(url).reply(200, JSON.parse(response)) next()

    @Given /^"([^"]*)" returns this response:$/, fakeAPIResponse @After "@API", (next) -> @mock.done() next() A SIMPLE NOCK EXAMPLE