13 July 2015

Installation

https://github.com/rspec/rspec-rails

Add rspec-rails to both the :development and :test groups in the Gemfile:
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end

Download and install by running:

bundle install

Initialize the spec/ directory (where specs will reside) with:

rails generate rspec:install

Test with Kaminari pagination

assign(:activities, Kaminari.paginate_array([@activity, @activity2]).page(1))

Test views with partials

If Rspec throws the following error:

Failure/Error: render
ActionView::Template::Error:
  No route matches {:action=>"add_loop", :controller=>"areas"} missing required keys: [:id]

use stub_template

# _form.html.erb
...
<%= render partial: 'add_loops', locals: { area: @area } if @area.id %>
...
</code

<code>
RSpec.describe "areas/edit", :type => :view do
  before(:each) do
    @area = assign(:area, Area.create!(
      :name => "MyString"
    ))
  end

  it "renders the edit area form" do
    stub_template "_add_loops.html.erb" => "add_loops"
    stub_template "_add_pipes.html.erb" => "add_pipes"
    render

    assert_select "form[action=?][method=?]", area_path(@area), "post" do

      assert_select "input#area_name[name=?]", "area[name]"
    end
  end
end