I have a (still) simple Rails application for books, and I want to test that the "show" page for an individual book contains that book's title.
The show.html.erb template is quite simple still, and as part of it, <%= @book.title %> is printed. However, RSpec is struggling before that. Here's the full code of show.html.erb_spec.rb:
require 'rails_helper'
RSpec.describe "books/show.html.erb", type: :view do
context 'Show page' do
let(:book) { create(:hobbit) }
end
it 'displays the book title on the show page' do
assign(:book, book)
render
expect(rendered).to have_content(book.title)
end
end
The factory for "hobbit" looks like this:
FactoryBot.define do
factory :hobbit, class: Book do
title { 'The Hobbit' }
year { 1937 }
rating { 5 }
condition { 4 }
end
...
end
And the error I get is relating to the "assign" statement in the spec. I don't get what the problem is - the show page should know an instance variable @book?
books/show.html.erb
displays the book title on the show page (FAILED - 1)
Failures:
1) books/show.html.erb displays the book title on the show page
Failure/Error: assign(:book, book)
NameError:
undefined local variable or method `book' for #<RSpec::ExampleGroups::BooksShowHtmlErb "displays the book title on the show page" (./spec/views/books/show.html.erb_spec.rb:7)>
# ./spec/views/books/show.html.erb_spec.rb:8:in `block (2 levels) in <top (required)>'
I am sure this is a very stupid beginner error but I couldn't find anything that helps me with this?
The simple problem was that I didn't install capybara!