[Ruby on Rails 4] Cucumber を試す

どんなものかさわってみた。

Gemfile

group :test do
  gem 'cucumber-rails', '1.2.1', :require => false
  gem 'database_cleaner', '0.7.0'
end
% bundle
% rails generate cucumber:install
      create  config/cucumber.yml
      create  script/cucumber
       chmod  script/cucumber
      create  features/step_definitions
      create  features/support
      create  features/support/env.rb
       exist  lib/tasks
      create  lib/tasks/cucumber.rake
        gsub  config/database.yml
        gsub  config/database.yml
       force  config/database.yml

## 使い方
### フィーチャーファイルの作成

% subl features/signing_in.feature

Feature: Signing in
Scenario: Unsuccessful signin
    Given a user visits the signin page
    When he submits invalid signin information
    Then he should see an error message
Scenario: Successful signin
    Given a user visits the signin page
      And the user has an account
    When the user submits valid signin information
    Then he should see his profile page
      And he should see a signout link

ステップファイルの作成

% subl features/step_definitions/authentication_steps.rb
Given /^a user visits the signin page$/ do
  visit signin_path
end
When /^he submits invalid signin information$/ do
  click_button "Sign in"
end
Then /^he should see an error message$/ do
  page.should have_selector('div.alert.alert-error')
end
Given /^the user has an account$/ do
  @user = User.create(name: "Example User", email: "user@example.com",
                      password: "foobar", password_confirmation: "foobar")
end
When /^the user submits valid signin information$/ do
  fill_in "Email",    with: @user.email
  fill_in "Password", with: @user.password
  click_button "Sign in"
end
Then /^he should see his profile page$/ do
  page.should have_selector('title', text: @user.name)
end
Then /^he should see a signout link$/ do
  page.should have_link('Sign out', href: signout_path)
end

### テストの実行

% bundle exec cucumber features/
Using the default profile...
Feature: Signing in
Scenario: Unsuccessful signin # features/signing_in.feature:3
Given a user visits the signin page # features/step_definitions/authentication_steps.rb:1
When he submits invalid signin information # features/step_definitions/authentication_steps.rb:5
Then he should see an error message # features/step_definitions/authentication_steps.rb:9
Scenario: Successful signin # features/signing_in.feature:8
Given a user visits the signin page # features/step_definitions/authentication_steps.rb:1
And the user has an account # features/step_definitions/authentication_steps.rb:13
When the user submits valid signin information # features/step_definitions/authentication_steps.rb:18
Then he should see his profile page # features/step_definitions/authentication_steps.rb:24
And he should see a signout link # features/step_definitions/authentication_steps.rb:28
2 scenarios (2 passed)
8 steps (8 passed)
0m0.882s

Cucumberのフィーチャーは非常に読みやすいのですが、その代わり実装されているコードから完全に切り離されていて、両刃の剣です。著者にとって、Cucumberは読みやすいが書くのは面倒、結合テストはプログラマーにとってはそれほど読みやすくない代わりに書くのははるかに楽、という印象です。

補遺

DatabaseCleaner