-
Notifications
You must be signed in to change notification settings - Fork 242
Quick Start Walkthrough
This page provides a quick and simple walkthrough of how to begin writing Javascript specs using Teaspoon and Jasmine.
- Create a new Rails app
- Install the Teaspoon Jasmine gem and bootstrap it with the generator (
rails g teaspoon:install
) - Write your first spec (walkthrough below)
- Run the test suite
- Red. green. refactor.
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.