Skip to content

Quick Start Walkthrough

turadg edited this page Feb 17, 2013 · 11 revisions

This page provides a quick and simple walkthrough of how to begin writing Javascript specs using Teabag.

  1. Create a new Rails app
  2. Install Teabag and bootstrap it with the generator (rails g teabag:install)
  3. Write your first spec (walkthrough below)
  4. Run the test suite
  5. Red. green. refactor.

Writing your first spec

The install generator will create a spec/javascripts directory for you. Teabag will automatically pick up any specs written in that folder named [anything]_spec.(js|coffee|js.coffee).

Let's write a basic implementation in CoffeeScript using Jasmine (though you could just as easily use vanilla Javascript). Start by creating a spec/javascripts/calculator_spec.coffee and add the following:

#= require calculator
describe "Calculator", ->

  it "should add two digits", ->
    expect( new Calculator().add(2,2) ).toBe(4)

Now let's create an app/assets/javascripts/calculator.coffee and add:

class @Calculator

Run rake teabag - you should see your first failing spec.

Failures:

  1) Calculator should add two numbers.
     Failure/Error: TypeError: 'undefined' is not a function

To make the test pass we just need to implement the add method.

  add: (a, b) ->
    a + b

rake teabag again and that spec should be passing!

If you'd prefer, you can also run your tests in the browser. Fire up your Rails server and visit localhost:3000/teabag to run the specs in whichever browser you want.

Testing your application assets

Teabag supports the assets pipeline, but does not automatically load any. To load any application Javascript asset, you have to specify it in the spec file or in spec_helper.coffee. (E.g. comment out the require application at the top of spec_helper.coffee.)