Ruby on Rails Tutorial (12)

Gitlab 4.1 導入を、と思っていたのですが、導入予定の仮想リソースに不具合があるらしく提供元に問合せ中でレスポンスがすぐではないと見込んで続きに着手。

8.3 Introduction to Cucumber (optional)

BDD なツールとのこと。とりあえず先に進みます。

8.3.1 Installation and setup

Gemfile の test なナニに以下のように指定せよ、とのこと。

  gem 'cucumber-rails', '1.2.1', :require => false
  gem 'database_cleaner', '0.7.0'

これは一旦、これまでのソレな commit を作っておいた方が良いな。あ、8.4 で全部まるっとヤッてしまってるみたい、ですがやっぱ commit は作ろう。
上を盛り込む前に以下。

$ git commit -a -m 'Chapter 8 Sign in, sign out'

で、盛り込み再開。
とりあえず Gemfile 更新したら以下。

$ bundle install

で、以下を、とありますね。で、それが済んだら以下らしい。

$ 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
8.3.2 Features and steps

あら、どっかで見たと思ったら cucumber てインフラ UT なソレで見たんだったな。とりあえずどんどん盛り込んでみます。
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
      And the user submits valid signin information
    Then he should see his profile page
      And he should see a signout link

で、以下で試験実行なのかな。

$ bundle exec cucumber features/

あら、どうなったのかよく分からんぞ。なんとなくパスしてなさげ。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...
Rack::File headers parameter replaces cache_control after Rack 1.5.
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
    And 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)
0m2.592s

これ、面白いな。で、最後にカスタムマッチャが出てくるのか。

8.3.3 Counterpoint: RSpec custom matchers

cucumber は関係なくって RSpec なのか。つうかここ、面白いんですが rspec なカスタムマッチャって cucumber に流用できるんですか?
ちょい色々な意味で限界なので続きは明日以降とゆーことで。Tutorial が週末対応で終わったら嬉しいのですが Gitlab 云々な割り込みが出てくると微妙。