-
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 Teabag.
- Create a new Rails app
- Install Teabag and bootstrap it with the generator (
rails g teabag: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. 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.
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
.)