user773327
user773327

Reputation: 51

undefined local variable or method `render'

I am getting the following error in my rspec code

undefined local variable or method `render'

here is my code:

require 'spec_helper'
describe "messages/show.html.erb" do
   it "displays the text attribute of the message" do
    render
    rendered.should contain("Hello world!")
  end
end

this is from the book : The RSpec book below is what i added to Gemfile.

group :development , :test do
   gem "rspec-rails", ">= 2.0.0"
   gem "webrat", ">= 0.7.2"
end

I have not added the file show.html.erb to the views/messages but i should be getting another error , not this

The strange thing is that this worked on my machine some time before. I deleted the project and created another one and now it just wouldn't work

I had issued these commands after editing the gemfile

bundle install
script/rails generate rspec:install
rake db:migrate

Upvotes: 3

Views: 4048

Answers (4)

l2silver
l2silver

Reputation: 3439

For me the answer was to change

describe "messages/show.html.erb" do

to

describe "messages/show.html.erb", type: :view do

Upvotes: 10

noob in tights
noob in tights

Reputation: 61

If your show.html.erb_spec.rb file contains:

require 'spec_helper.rb'

try changing it to:

require 'rails_helper.rb'

Explanation: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade

Upvotes: 6

Matthias
Matthias

Reputation: 4375

you could change your test like following:

require 'spec_helper'
describe "messages/show.html.erb" do
  it "displays the text attribute of the message" do
    FactoryGirl.create(:message) # or an other test data creator gem
    visit "/messages/1"
    page.should have_content("Hello world!")
  end
end

Upvotes: 0

user2313077
user2313077

Reputation: 1

It looks like you're missing a closing quote on the first line of your code:

require 'spec_helper

Upvotes: 0

Related Questions