3

I'm trying to follow Michael Hartl's Rails 3 tutorial and I'm running into an error I can't find explained elsewhere. Under Section 9.2.1, I'm getting an error when trying to run a test for non-signed-in users trying to update a profile page.

describe "submitting to the update action" do
  before { put user_path(user) }
  specify { response.should redirect_to(signin_path) }
end

The error I'm getting is "undefined local variable or method 'response' for # (NameError)". This is a response to "bundle exec rspec spec/". When I comment out the offending line, all tests instantiate and pass. I've tried replacing "response" with things like "response.body" and "page" but neither seem to help.

I'm not sure what the issue is because as I'm moving forward with the tutorial, similar blocks are passing without incident.

Here's the full error text:

C:\Ruby193\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:\Ruby193\bin\rake spec
Testing started at 7:39 PM ...
C:/Ruby193/bin/ruby.exe -S rspec ./spec/models/user_spec.rb ./spec/requests/authentication_pages_spec.rb ./spec/requests/static_pages_spec.rb ./spec/requests/user_pages_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:60:in `block (5 levels) in <top (required)>': undefined local variable or method `response' for #<Class:0x4894908> (NameError)
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
    from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:58:in `block (4 levels) in <top (required)>'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
    from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:51:in `block (3 levels) in <top (required)>'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
    from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:48:in `block (2 levels) in <top (required)>'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
    from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:46:in `block in <top (required)>'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/dsl.rb:18:in `describe'
    from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:3:in `<top (required)>'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
rake aborted!
C:/Ruby193/bin/ruby.exe -S rspec ./spec/models/user_spec.rb ./spec/requests/authentication_pages_spec.rb ./spec/requests/static_pages_spec.rb ./spec/requests/user_pages_spec.rb failed
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => spec
(See full trace by running task with --trace)

Process finished with exit code 1
Empty test suite.

I'm pretty sure it's not a typo somewhere but here's the offending file (authentication_pages_spec.rb). (have_title() is a method I found here for fixing an error because apparently have_selector('title',...) is no longer valid syntax.)

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it {should have_selector('h1', text: 'Sign in') }
    it {should have_title('Sign in') }
  end

  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_title('Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      it { should have_title(user.name) }
      it { should have_link('Profile', href: user_path(user)) }
      it { should have_link('Settings', href: edit_user_path(user)) }
      it { should have_link('Sign out', href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }

      describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
      end
    end
  end

  describe "authorization" do

    describe "for non-signed-in users" do
      let(:user) { FactoryGirl.create(:user) }

      describe "in the Users controller" do

        describe "visiting the edit page" do
          before { visit edit_user_path(user) }
          it { should have_title('Sign in') }
        end

        describe "submitting to the update action" do
          before { put user_path(user) }
          specify ( response.should redirect_to(signin_path) )
        end

        describe "as wrong user" do
          let(:user) { FactoryGirl.create(:user) }
          let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
          before { sign_in user }

            describe "visiting Users#edit_page" do
              before { visit edit_user_path(wrong_user) }
              it { should_not have_title(full_title("Edit user")) }
            end

            describe "submitting a PUT request to the Users#update action" do
              before { put user_path(wrong_user) }
              specify { response.should redirect_to(root_path) }
            end
         end
      end
    end
  end
end
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
Artiavis
  • 45
  • 1
  • 8
  • 1
    stick to gem versions as specified here: http://ruby.railstutorial.org/chapters/static-pages#code-gemfile_rspec – prusswan Jan 20 '13 at 01:26
  • I copypastad the source gemfile and am still getting this error – Artiavis Jan 20 '13 at 03:16
  • This doesn't answer your question, but I just had the same error message for a different reason so I'm posting this for anyone else with the same problem: I'd forgotten to include the line `subject { page }`. D'oh! – GMA Aug 12 '13 at 06:47

1 Answers1

2

What version of Capybara are you running?

$ bundle exec gem list | grep capybara

If you're running version >= 2.0 you should look again at your Gemfile and specifically mark the version to be the same as the tutorial (1.1.2).

Gemfile

group :test do
  gem 'capybara', '1.1.2'
end

If you desperately want to use the latest version, you'll have to make some changes, outlined in the following StackOverflow Q&As:

Update

Okay, you're going to kick yourself, but after cloning your repo and getting the same error, your problem is obvious (syntax highlighting is your friend):

On Line 77 of your spec/requests/authentication_pages_spec.rb you have:

specify ( response.should redirect_to(signin_path) )

It should be:

specify { response.should redirect_to(signin_path) }

After fixing that, your tests will pass.

Community
  • 1
  • 1
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
  • Still on version 1.1.2 per the Gemfile in the tutorial – Artiavis Jan 28 '13 at 05:59
  • Do you have a Github repo with the version of the code you're working on? Given that you say "apparently `have_selector('title',...)` is no longer valid syntax" leads me to think you didn't specify a Capybara version when you first ran `bundle install` and got Capybara version 2. If that is the case and you changed back to version 1.1.2 later, did you run `bundle install` again? – Paul Fioravanti Jan 28 '13 at 06:34
  • I double checked, reran `bundle install`, and still the error. https://github.com/Artiavis/sample_app – Artiavis Jan 29 '13 at 04:52