Rspec and autotest

September 17th, 2007

I thought I’d mention this briefly as rspec is becoming a very popular way to test. I was manually running my tests until I was recently pointed in the direction of auto-test with spec_server. This significantly increases the feedback loop for when your tests pass and fail; increasing productivity exponentially. The steps are as follows:

Install ZenTest:

sudo gem install ZenTest

Install rspec for rails:

http://rspec.rubyforge.org/documentation/rails/install.html

Open up a terminal and open run script/spec_server from the root of your rails application. Then open up another terminal and run autotest -rails (also inside of the root of the rails application). Edit a spec to make it fail, save it and watch autotest tell you it failed! I know you can get this to work with growl (I’m on Ubuntu so I don’t use growl). If someone with OS X experience can comment on how to get growl integrated and perhaps provide an alternative for linux, I will post it up.

David Chelimsky announced today that an experimental version of the story runner for rspec has been committed to the trunk. This move is clearing the path which rspec is taking towards merging the rspec and rbehave frameworks. The code example he gave for this new feature is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# The following is based on an experimental Rails-Story adapter
# in RSpec's trunk at rev2480.
#
# You run it by standing in RAILS_ROOT and saying:
#   >ruby stories/add_person.rb
#
# Please forgive the lack of RESTful convention (get '/people/create' instead of '/people/new') -
# this works with the existing example app, which is, well, OLD.


# in RAILS_ROOT/stories/helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'spec/rails/story_adapter'

# in RAILS_ROOT/stories/add_person.rb
require File.join(File.dirname(__FILE__), "helper")

Story "Add Person", %{
  As an admin
  I want to add people to the system
  So that I show how many people use my system
}, :type => RailsStory do
  Scenario "Successfully add person" do
    Given "no people in the system" do
      Person.destroy_all
    end
    
    When "creating a new person named", "Dan" do |name|
      post "/people/create", :person => {:name => name}
    end
    
    Then "viewer should see", "/people/list" do
      follow_redirect!
      response.should render_template("people/list")
    end
    
    Then "list should include", "Dan" do
      response.should have_text(/Dan/)
    end
  end
  
  Scenario "Redirect to create form on failed create" do
    Given "no people in the system" do
      Person.destroy_all
    end
    
    When "creating a new person with no name" do
      post "/people/create", :person => {:name => nil}
    end
    
    Then "viewer should see", "/people/create" do
      assert_template "people/create"
    end
    
    Then "list should not include", "Dan" do
      response.should_not have_text(/Dan/)
    end
  end
end