トップ «前の日記(2010-09-27(月)) 最新 次の日記(2010-10-02(土))» 編集
RSS feed

皮肉な日々と心意気


2010-09-29(水)

Rails3 その3 Steak+Capybaraで受け入れテスト (2)

Rails3で作ったプロジェクトにSteakとCapybaraを組み込む

プロジェクトルートにあるGemfileを編集。

% git diff Gemfile
diff --git a/Gemfile b/Gemfile
index 7556e59..25f9c3c 100644
--- a/Gemfile
+++ b/Gemfile
@@ -7,4 +7,6 @@ gem 'sqlite3-ruby', :require => 'sqlite3'
 group :development, :test do
   gem 'rspec-rails', '>= 2.0.0.beta.22'
   gem 'autotest'
+  gem 'steak', '>= 1.0.0.beta.1'
+  gem 'capybara'
 end

bundleインストールする

% bundle install
Fetching source index for http://rubygems.org/
(Using行は省略)
Installing nokogiri (1.4.3.1) with native extensions
Installing ffi (0.6.3) with native extensions
Installing json_pure (1.4.6)
Installing rubyzip (0.9.4)
Installing selenium-webdriver (0.0.28)
Installing capybara (0.3.9)
Installing steak (1.0.0.beta.2)

generatorを実行

% rails g steak:install
Defaulting to Capybara...
      create  spec/acceptance/support
      create  spec/acceptance/acceptance_helper.rb
      create  spec/acceptance/support/helpers.rb
      create  spec/acceptance/support/paths.rb

準備完了。

specを書いてみる

単純な機能を作ってみる。Railsアプリケーションの/authorsにアクセスすると、著者一覧が表示されるようにする。

% r g model author name:string
      invoke  active_record
      create    db/migrate/20100929081958_create_authors.rb
      create    app/models/author.rb
      invoke    rspec
      create      spec/models/author_spec.rb
% rake db:migrate
% r g steak:spec authors
       exist  spec/acceptance
      create  spec/acceptance/authors_spec.rb

最低限のspec書いて実行。

% cat spec/acceptance/authors_spec.rb
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/acceptance_helper'

feature "Authors" do
  scenario "著者一覧の表示" do
    visit '/authors'
    page.should have_content('著者一覧')
  end
end
% rspec spec/acceptance/authors_spec.rb
F

Failures:
  1) Authors 著者一覧の表示
     Failure/Error: page.should have_content('著者一覧')
     expected #has_content?("著者一覧") to return true, got false
     # ./spec/acceptance/authors_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.43047 seconds
1 example, 1 failure

正しくテスト失敗した。controllerをgenerate。

% r g controller authors index
      create  app/controllers/authors_controller.rb
       route  get "authors/index"
      invoke  erb
      create    app/views/authors
      create    app/views/authors/index.html.erb
      invoke  rspec
      invoke  helper
      create    app/helpers/authors_helper.rb
      invoke    rspec
      create      spec/helpers/authors_helper_spec.rb

config/routes.rbを編集。

Emkuma::Application.routes.draw do
  resources :authors, :only => [:index]
end

app/views/authors/index.html.erbを編集してからspec実行

% rspec spec/acceptance/authors_spec.rb
.

Finished in 0.42275 seconds
1 example, 0 failures

これで、

  • /authorsにアクセスしたときにauthors#indexにルーティング
  • その結果表示されるビュー

の最低限のテストが通った、と。

さらにspecを書いてみる

次にauthorsテーブルからちゃんとレコードを取り出していることをテストする。 modelは生成済み。

% cat db/schema.rb
ActiveRecord::Schema.define(:version => 20100929081958) do

  create_table "authors", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "comics", :force => true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

fixtureを用意する。この時点でspec/fixturesディレクトリって自動で生成されていないのか。

% cat spec/fixtures/authors.yml
isikei:
  name: 石恵

cuvie:
  name: Cuvie

specを再編集して、実行。

% cat spec/acceptance/authors_spec.rb
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/acceptance_helper'

feature "Authors" do

  fixtures :authors

  scenario "著者一覧の表示" do
    visit '/authors'
    page.should have_content('著者一覧')
    page.should have_content('Cuvie')
    page.should have_content('石恵')
  end
end
% rspec spec/acceptance/authors_spec.rb
F

Failures:
  1) Authors 著者一覧の表示
     Failure/Error: page.should have_content('Cuvie')
     expected #has_content?("Cuvie") to return true, got false
     # ./spec/acceptance/authors_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.43361 seconds
1 example, 1 failure

きちんとテスト失敗した。では、controllerを編集。

% cat app/controllers/authors_controller.rb
class AuthorsController < ApplicationController
  def index
    @authors = Author.all
  end

end

つづいてviewを編集。

% cat app/views/authors/index.html.erb
<h1>著者一覧</h1>

<table class="authors">
  <col class="name" />
  <tr>
    <th>名前</th>
  </tr>
  <%= render @authors %>
</table>
% cat app/views/authors/_author.html.erb
<tr>
  <td><%= author.name %></td>
</tr>

再度spec実行。

% rspec spec/acceptance/authors_spec.rb
.

Finished in 0.52989 seconds
1 example, 0 failures

通った。

自分でブラウザを操作して確認してみよう。としたら問題が。

% rake db:fixtures:load
(in /Users/sen/code/rails/emkuma)

% r c
Loading development environment (Rails 3.0.0)
ruby-1.9.2-p0 > Author.count
 => 0

rake db:fixtures:load してもdevelopment環境にfixturesがロードされていないぞ。なぜだ。

試しにtest/fixturesにコピーしたら、ちゃんとロードされたよ。rspec-railsのインストールに失敗でもしてるのかな。

% mkdir -p test/fixtures
% cp spec/fixtures/authors.yml test/fixtures
% rake db:fixtures:load
(in /Users/sen/code/rails/emkuma)
% r c
Loading development environment (Rails 3.0.0)
ruby-1.9.2-p0 > Author.count
 => 2
Tags: Rails

トップ «前の日記(2010-09-27(月)) 最新 次の日記(2010-10-02(土))» 編集
RSS feed