【问题标题】:Rails Tests Not Found - Undefined local variable or method "response"未找到 Rails 测试 - 未定义的局部变量或方法“响应”
【发布时间】:2013-01-20 00:58:10
【问题描述】:

我正在尝试关注 Michael Hartl 的 Rails 3 tutorial,但遇到了一个在其他地方找不到解释的错误。在 Section 9.2.1 下,我在尝试为尝试更新个人资料页面的未登录用户运行测试时遇到错误。

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

我得到的错误是“# (NameError) 的未定义局部变量或方法'响应'”。这是对“bundle exec rspec spec/”的回应。当我注释掉有问题的行时,所有测试都会实例化并通过。我试过用“response.body”和“page”之类的东西替换“response”,但似乎都没有帮助。

我不确定问题出在哪里,因为随着我继续学习本教程,类似的块正在顺利通过。

这是完整的错误文本:

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.

我很确定这不是某个地方的拼写错误,但这是有问题的文件 (authentication_pages_spec.rb)。 (have_title() 是我在这里找到的一种修复错误的方法,因为显然 have_selector('title',...) 不再是有效的语法。)

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

【问题讨论】:

  • 坚持使用此处指定的 gem 版本:ruby.railstutorial.org/chapters/static-pages#code-gemfile_rspec
  • 我将源 gemfile 复制粘贴,但仍然出现此错误
  • 这不能回答你的问题,但我只是出于不同的原因收到了相同的错误消息,所以我将这个发布给其他有同样问题的人:我忘记包含该行subject { page }。哦!

标签: ruby-on-rails-3 syntax rspec2 railstutorial.org


【解决方案1】:

您运行的是哪个版本的 Capybara?

$ bundle exec gem list | grep capybara

如果您正在运行版本 &gt;= 2.0,您应该再次查看您的 Gemfile,并专门将版本标记为与教程相同 (1.1.2)。

宝石文件

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

如果您迫切地想要使用最新版本,则必须进行一些更改,如下 StackOverflow 问答中所述:

更新

好吧,你会自责的,但是在克隆你的 repo 并得到同样的错误之后,你的问题很明显(语法突出显示是你的朋友):

Line 77 of your spec/requests/authentication_pages_spec.rb 你有:

specify ( response.should redirect_to(signin_path) )

应该是:

specify { response.should redirect_to(signin_path) }

修复该问题后,您的测试将通过。

【讨论】:

  • 教程中的 Gemfile 仍然是 1.1.2 版
  • 你有一个 Github 存储库,其中包含你正在处理的代码版本吗?鉴于您说“显然have_selector('title',...) 不再是有效的语法”让我认为您在第一次运行bundle install 并获得 Capybara 版本 2 时没有指定 Capybara 版本。如果是这种情况并且您改回1.1.2以后的版本,你又跑bundle install了吗?
  • 我再次检查,重新运行bundle install,仍然是错误。 github.com/Artiavis/sample_app
猜你喜欢
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2017-08-02
  • 2014-07-06
相关资源
最近更新 更多