rspec + capybara undefined局部变量或方法

时间:2013-10-27 20:47:02

标签: ruby-on-rails rspec capybara

#gemfile
...
gem 'rspec-rails'
gem 'capybara'

我已根据official docs在spec_helper.rb中添加了'capybara / rails'。 我已经生成了一个测试:

$rails generate integration_test authentication_pages

我写了一个测试:

#spec/features/authentication_pages_spec.rb
describe "Authentication" do
  subject { page }    
  describe "signin" do    
    before { visit new_user_session_path }    
    describe "with invalid information" do
      before { click_button "Sign in" }    
      it { should have_title('Sign in') }
    end
  end
end

运行测试时出现错误:

$rspec spec/features/authentication_pages_spec.rb 
F

Failures:

  1) Authentication signin with invalid information 
     Failure/Error: before { visit new_user_session_path }
     NameError:
       undefined local variable or method `new_user_session_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x000000010f9618>
     # ./spec/features/authentication_pages_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.0007 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/authentication_pages_spec.rb:11 # Authentication signin with invalid information

new_user_session_path是一个有效的路径,我在我的应用程序中使用它并且它可以工作。

我没有解决方案,我尊重官方文档。 你能救我吗?

1 个答案:

答案 0 :(得分:1)

我发现了错误: 缺少spec / features / authentication_pages_spec.rb中的'spec_helper'

正确的文件是这样的:

#spec/features/authentication_pages_spec.rb
require 'spec_helper'
describe "Authentication" do
  subject { page }    
  describe "signin" do    
    before { visit new_user_session_path }    
    describe "with invalid information" do
      before { click_button "Sign in" }    
      it { should have_title('Sign in') }
    end
  end
end