簡體   English   中英

rspec + capybara未定義的局部變量或方法

[英]rspec+capybara undefined local variable or method

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

我已經根據官方文檔在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是有效路徑,我在我的應用程序中使用了它並且有效。

我沒有解決方案,我尊重官方文檔。 你能幫助我嗎?

我發現了錯誤:在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM