LoginSignup
3
3

More than 5 years have passed since last update.

RubyMineでやってみた:『スはスペックのス 【第 2 回】 RSpec の概要と、RSpec on Rails (コントローラ、ビュー編)』

Last updated at Posted at 2013-03-14
Component version
RubyMine 5.0(Windows)
Rails 3.2.12
Rspec 2.13.0
Capybara 1.1.4

Goal

ブログのトップページを表示する。表示する情報はブログの情報のみ。

Setup

  1. Tools > Run Rails Generator > controller > blogs show

  2. Run > Run > spec

  3. テストが走り、ペンディング(黄色)になる

  4. Toggle auto-testをオンにする

  5. テストを追加する

spec/controllers/blog_controller_spec.rb
# encoding: utf-8
require 'spec_helper'

describe BlogsController do

  describe "GET 'show'" do
    it "returns http success" do
      get 'show'
      response.should be_success
    end
  end

  fixtures :blogs
  context "fixtureを使った場合:" do
    describe "fixtureの1番目のエントリをgetしたとき" do
      it "リクエストは成功すること" do
        get 'show', id: blogs(:one).id
        response.should be_success
      end
    end
  end
end
  1. 自動でテストが走り、通過する

1st Red

  1. テストを追加する
spec/controllers/blog_controller_spec.rb
    it ":idで指定したブログをロードしていること" do
      get 'show', :id => blogs(:one).id
      assigns[:blog].should == blogs(:one)
    end
  1. 自動でテストが走り、コケる

  2. コードを書く

app/controllers/blog_controller.rb
...
@blog = Blog.find(params[:id])
...
  1. 自動でテストが走り、コケる

「returns http success」

「Couldn't find Blog without an ID」

  1. 以下のテストを削除する ```ruby:spec/controllers/blog_controller_spec.rb describe BlogsController do

describe "GET 'show'" do
it "does not return http success" do
get 'show'
response.should_not be_success
end
end
end
```

  1. 自動でテストが走り、通過する

  2. テストをリファクタリングする

spec/controllers/blog_controller_spec.rb
# encoding: utf-8
require 'spec_helper'

describe BlogsController do

  fixtures :blogs
  context "fixtureを使った場合:" do
    before do
      @blog = blogs(:one)
      get 'show', :id => @blog.id
    end

    describe "fixtureの1番目のエントリをgetしたとき" do

      it "リクエストは成功すること" do
        response.should be_success
      end

      it ":idで指定したブログをロードしていること" do
        assigns[:blog].should == blogs(:one)
      end
    end
  end
end
  1. 自動でテストが走り、通過する

2nd Red

  1. テストを追加する
spec/controllers/blog_controller_spec.rb
it "blogs/showを描画すること" do
  response.should render_template("blogs") # 間違えている
end
  1. 自動でテストが走り、コケる

  2. テストを修正する

spec/controllers/blog_controller_spec.rb
it "blogs/showを描画すること" do
  response.should render_template("blogs/show")
end
  1. 自動でテストが走り、通過する

3rd Red

  1. Capybara gemを入れる(have_selector関数を使いたい)
Gemfile
gem "capybara", :group => [:development, :test]
  1. bundle install

  2. テストを追加する

spec/views/show.html.erb_spec.rb
# encoding: utf-8
require 'spec_helper'

describe "blogs/show.html.erb" do

  context "fixtureを使った場合:" do
    fixtures :blogs

    describe "/blogs/showにアクセスしたとき" do
      before(:each) do
        @blog = assigns[:blog] = blogs(:one)
        render(template: "blogs/show")
      end

      it "ブログの名前を表示すること" do
        response.should have_selector('div.blog > h1', :content=>@blog.name)
      end
    end
  end
end
  1. 自動でテストが走り、コケる

  2. コードを書く

app/views/show.html.erb
<div class="blog">
  <h1><%=h @blog.name %></h1>
</div>
  1. 自動でテストが走り、通過する

おしまい。


ブログやってます:PAPA-tronix !

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3