Steak を Request Specs + Capybara に変更してみた

WEB+DB PRESS Vol.61の 「Rails 3テスト最前線」を読み Steak を使わなくても RSpec 2.0 の新機能 Request Specs と Capybara を組み合わせる事で Steak 同様にテストが書ける事が判ったので試してみた。 Steakに付いては、ここ

WEB+DB PRESS Vol.61

WEB+DB PRESS Vol.61

環境の変更

1. Gemfile からはSteakを除く

group :test, :development do
  gem "rspec-rails"
  gem 'capybara'
  gem 'spork'
end

2. spec_helper.tb に capybara/rails を追加

 ・・・
require 'rspec/rails'
require 'capybara/rails'
 ・・・

3. steakの helpers.rb, paths.rb を spec/support に移動し :type を変更

module NavigationHelpers
  def list_page
    "/todos"
  end
  
  def new_page
    "/todos/new"
  end

  def edit_page(id)
    "/todos/#{id}/edit"
  end

end

RSpec.configuration.include NavigationHelpers, :type => :request

RSpec コードの変更

変更はごくわずかで

  • feature → describe
  • scenario → it
  • require File.expand_path..... → require 'spec_helper'
  • spec/acceptance/*_spec.rb → sepc/requests に移動

require 'spec_helper'

describe "Todo一覧の表示" do
  fixtures :todos

  it "Todo一覧が表示できる" do

    visit list_page
    
    page.should have_css("h1", :text => "Listing todos")
    page.all("table tr:eq(2) td")[0..1].map{|e| e.text}.should == ["2011-02-18", "打ち合わせ"]
    page.all("table tr:eq(3) td")[0..1].map{|e| e.text}.should == ["2011-02-20", "開発"]
  end
end

感想

Request Specsを使うと RSpecの記述になるだけです :-)

ようは RSpecの語彙が良い(好き) か Steak の 語彙が良いかの判断だと思います。プログラマーが読み書きするだけなら、RSpecの語彙を使った方がプログラマーにとっては良いかなと私は思いました。そして現在開発中のプロジェクトでは Request Specs を使う事にしました。