Skip to content

Using With RSpec View Specs

Vinicius Ferreira Negrisolo edited this page May 11, 2016 · 2 revisions

If you have a code like this:

# app/controllers/posts_controller.rb
class PostsController < ApplicationController
  expose :posts, ->{ Post.all }
end
<!-- app/views/posts/index.html.erb -->
<table>
  <tr><td>Title</td></tr>

  <% posts.each do |post| %>
    <tr><td><%= post.title %></td></tr>
  <% end %>
</table>

You can stub your `posts` like this:

# spec/views/posts/index.html.erb_spec.rb
require "rails_helper"

RSpec.describe "posts/index" do
  it "displays all posts" do
    expect(controller).to receive(:posts).and_return([Post.new(title: 'Foo')])

    render

    expect(rendered).to match /Foo/
  end
end