Bazley
Bazley

Reputation: 2847

rspec giving incorrect error messages

Following the RailsTutorial, I keep getting this error message (one of four actually) when it should be working:

UsersController authentication of edit/update pages for signed-in 
users should require matching users for 'edit'
Failure/Error: @users = [@user, second, third] undefined local variable
or method 'second' for
#<RSpec::Core::ExampleGroup::Nested_3::Nested_7::Nested_2:0x000001040dcb68> #
./spec/controllers/users_controller_spec.rb:275:in 'block
(4 levels) in <top (required)>'

I have tried to fix it by looking at other questions on stack overflow but to no avail. My gemfile now looks like this:

gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
gem 'gravatar_image_tag', '0.1.0'
gem 'will_paginate', '3.0.pre2'

group :development do
  gem 'rspec-rails', '2.0.1'
  gem 'annotate-models', '1.0.4'
  gem 'faker', '0.3.1'
end

group :test do
  gem 'rspec-rails', '2.0.1'
  gem 'webrat', '0.7.1'
  gem 'spork', '0.8.4'
  gem 'factory_girl_rails', '1.0'
end

Any suggestions would be greatly appreciated!

Upvotes: 1

Views: 203

Answers (2)

Jason Morrison
Jason Morrison

Reputation: 172

The backtrace is coming from the users_controller_spec. Without the source, it's hard to say, but I'm guessing it's something like this:

describe UsersController
  before do
    @user = Factory(:user)
    sign_in(@user)
  end

  context "GET to edit for another user" do
    before do 
      @other_user = Factory(:user)
      get :edit, :id => @other_user
    end

    it "should disallow editing of other users" do
      # ... expectations here of HTTP 401 or a user-friendly message
    end
  end
end

Note that the variables shared between #before blocks and the examples are instance variables - local variables wouldn't be shared across them. I'm guessing your code might be trying to share a local between examples, or between a #before block and its examples.

Upvotes: 2

idlefingers
idlefingers

Reputation: 32067

I'm guessing second and third don't exist when you're calling [@user, second, third] (well, more than guessing. Its what the error says ;) ). They're probably in the tutorial to suggest you could be handling multiple objects in this code. Try removing them.

If that's not it, can you post a link to the tutorial you're following, or provide more background about where second and third are supposed to be coming from?

Upvotes: 1

Related Questions