Skip to content

Quick Start Walkthrough

Jeremy Jackson edited this page Mar 10, 2016 · 11 revisions

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

  1. Create a new Rails app
  2. Install the Teaspoon Jasmine gem and bootstrap it with the generator (rails g teaspoon: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. Teaspoon 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 numbers", ->
    expect( new Calculator().add(2,2) ).toBe(4)

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

class @Calculator

Run rake teaspoon - 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 teaspoon 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/teaspoon to run the specs in whichever browser you want.