2017-02-24 46 views
0

我使用RSpec的護欄3.5.2,2.5.0水豚,導軌4.2.4Rails的在功能測試未加載路線

這是我的測試:

#spec/features/visitors/sign_up_spec.rb 
feature 'Sign Up' do 

    scenario 'visitor can sign up with valid email address and password' do 
    visit new_user_registration_path 
    fill_in 'user_email', with: "#{SecureRandom.hex}}@example.com" 
    fill_in 'user_password', with: "password" 
    click_button 'Sign up' 

    #other code here 
    end 
end 

new_user_registration_path方法不存在這是錯誤:

$ rspec spec/features/visitors/sign_up_spec.rb                              
F 
Failures: 

    1) Sign Up visitor can sign up with valid email address and password 
    Failure/Error: visit new_user_registration_path 

    NoMethodError: 
     undefined method `empty?' for nil:NilClass 
    # ./spec/features/visitors/sign_up_spec.rb:15:in `block (2 levels) in <top (required)>' 

Finished in 0.17765 seconds (files took 3.54 seconds to load) 
1 example, 1 failure 

Failed examples: 

rspec ./spec/features/visitors/sign_up_spec.rb:14 # Sign Up visitor can sign up with valid email address and password 

如果我嘗試visit '/'它的工作原理。

這是我.rspec文件

$ cat .rspec                                       
--color 
--require rails_helper 

Rails的路由不會被加載。

我用撬調查:

14: scenario 'visitor can sign up with valid email address and password' do 
    15:  binding.pry 
=> 16:  visit new_user_registration_path 
    17:  fill_in 'user_email', with: "#{SecureRandom.hex}}@example.com" 

[1] pry(#<RSpec::ExampleGroups::SignUp>)> new_user_registration_path 
NoMethodError: undefined method `empty?' for nil:NilClass 
from /home/user/.rvm/gems/[email protected]/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:769:in `optimize_routes_generation?' 
[2] pry(#<RSpec::ExampleGroups::SignUp>)> 

路線:

$ rake routes | grep "new_user_registration"                               
    new_user_registration GET  /users/sign_up(.:format)     my_devise/registrations#new 

這是從控制檯:

$ rails c test                                      
Running via Spring preloader in process 5692 
Loading test environment (Rails 4.2.4) 
2.2.6 :001 > Rails.application.routes.url_helpers.new_user_registration_path 
NoMethodError: undefined method `empty?' for nil:NilClass 

想法?

+0

假設你已經正確安裝RSpec的護欄應該是自動的 - https://github.com/rspec/rspec-rails/blob/master/lib/rspec /rails/example/feature_example_group.rb#L15。我會說這對你來說是正確的,因爲它不會告訴你xxx_path方法不存在,它告訴你方法有錯誤。檢查您的routes.rb文件是否有任何近期更改,並確保您沒有以某種方式搞砸它。 –

+0

,如果你看errror的位置 - https://github.com/rails/rails/blob/v4.2.4/actionpack/lib/action_dispatch/routing/route_set.rb#L769 - 你可以看到它的原因您沒有爲某種原因設置您的default_url_options。已經包含 –

回答

0

嘗試包括你spec/rails_helper.rb

require File.expand_path('../../config/environment', __FILE__) 
require 'rspec/rails' 
+0

,不起作用 – user1066183