第3章 ほぼ静的なページの作成 | Rails チュートリアル http://railstutorial.jp/chapters/static-pages?version=4.0#top

こちらを参考に実装し、躓いた点を列挙してみる。 つまずかなかったところは普通に飛ばしてます。

環境

  • Ruby 2.2.2
  • Rails 4.2.1

    1
    2
    
    $ rails new sample_app --skip-test-unit
    $ cd sample_app
     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
    
    source 'https://rubygems.org'
    ruby '2.0.0'
    #ruby-gemset=railstutorial_rails_4_0
    
    gem 'rails', '4.0.5'
    
    group :development, :test do
    gem 'sqlite3', '1.3.8'
    gem 'rspec-rails', '2.13.1'
    end
    
    group :test do
    gem 'selenium-webdriver', '2.35.1'
    gem 'capybara', '2.1.0'
    end
    
    gem 'sass-rails', '4.0.5'
    gem 'uglifier', '2.1.1'
    gem 'coffee-rails', '4.0.1'
    gem 'jquery-rails', '3.0.4'
    gem 'turbolinks', '1.1.1'
    gem 'jbuilder', '1.0.2'
    
    group :doc do
    gem 'sdoc', '0.3.20', require: false
    end
    
    group :production do
    gem 'pg', '0.15.1'
    gem 'rails_12factor', '0.0.2'
    end
    1
    2
    
    $ bundle install --without production
    $ bundle update

つまづき!

config/initializers/secret_token.rb なんてない。。と思ったので新規作成。。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
require 'securerandom'

def secure_token
  token_file = Rails.root.join('.secret')
  if File.exist?(token_file)
    # Use the existing token.
    File.read(token_file).chomp
  else
    # Generate a new token and store it in token_file.
    token = SecureRandom.hex(64)
    File.write(token_file, token)
    token
  end
end

SampleApp::Application.config.secret_key_base = secure_token
1
$ rails generate rspec:install

ここから本題

1
$ git checkout -b static-pages
1
$ rails generate controller StaticPages home help --no-test-framework

テスト駆動開発

1
$ rails generate integration_test static_pages
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end
end

Capybaraの設定追加

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# This file is copied to spec/ when you run 'rails generate rspec:install'
.
.
.
RSpec.configure do |config|
  .
  .
  .
  config.include Capybara::DSL
end
1
$ bundle exec rspec spec/requests/static_pages_spec.rb

つまづき!

なんかエラーでた

1
/.../sample_app/spec/spec_helper.rb:87:in `block in <top (required)>': uninitialized constant Capybara (NameError)

rails_spec.rbに書くのがいいらしい

1
2
3
4
5
6
7
RSpec.configure do |config|
...
  .
  .
  .
  config.include Capybara::DSL
end

テスト実行は出来たけど、テストでエラーが発生

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Failures:

  1) StaticPages GET /static_pages should have the content 'Sample App'
     Failure/Error: expect(page).to have_content('Sample App')
       expected #has_content?("Sample App") to return true, got false
     # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.32109 seconds (files took 6.05 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:5 # StaticPages GET /static_pages should have the content 'Sample App'

そもそも生成されたhtmlに’Sample App’が無いっていうエラー。

1
2
3
4
5
6
<h1>Sample App</h1>
<p>
    This is the home page for the
  <a href="http://railstutorial.jp/">Ruby on Rails Tutorial</a>
    sample application.
</p>

で解消。

ページの追加

チュートリアルでは直にコーディングしているが、あえてrails generateで作成してみる

1
2
3
4
$ rails generate controller StaticPa
ges about --no-test-framework
    conflict  app/controllers/static_pages_controller.rb
Overwrite /.../sample_app/app/controllers/static_pages_controller.rb? (enter "h" for help) [Ynaqdh]

コンフリクトしたのでnをおしてみる

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
        skip  app/controllers/static_pages_controller.rb
       route  get 'static_pages/about'
      invoke  erb
       exist    app/views/static_pages
      create    app/views/static_pages/about.html.erb
      invoke  helper
   identical    app/helpers/static_pages_helper.rb
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/static_pages.coffee
      invoke    scss
   identical      app/assets/stylesheets/static_pages.scss

コンフリクトしているファイル以外が生成されるみたい。

1
2
<h1>About Us</h1>
<p>Find me in app/views/static_pages/about.html.erb</p>

に変更してテスト

1
2
3
$ bundle exec rspec spec/requests/static_pages_spec.rb
Finished in 0.34315 seconds (files took 6.03 seconds to load)
2 examples, 0 failures

成功!