我已经开始研究Chapter 8 of the famous Rails tutorial了。我想我密切关注说明并定义了以下路线:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
会话控制器(/controllers/sessions_controller.rb)定义如下:
class SessionsController < ApplicationController
def new
end
def create
end
def destroy
end
end
在spec / requests / authentication_pages_spec.rb中,我创建了以下测试:
require 'spec_helper'
describe "Authentication" do
subject {page}
describe "signin page" do
before { visit signin_path}
it { should have_content('Sign in')}
it { should have_title('Sign in')}
end
end
测试导致以下错误:
Failures:
1) Authentication signin page
Failure/Error: before { visit signin_path}
NameError:
undefined local variable or method `signin_path' for #
<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec05fe8>
# ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Authentication signin page
Failure/Error: before { visit signin_path}
NameError:
undefined local variable or method `signin_path' for #
<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec5cf50>
# ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
即使在routes.rb中定义了 signin_path 命名路由,也无法识别它。我用其他一个(signup_path)替换了那个命名路由,问题就消失了。所以,它是关于这个特定命名路线的东西。你能说出问题所在吗?
rake路由产生以下输出:
sb7904313:sample_app nnikolo$ rake routes
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root GET / static_pages#home
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
我也重新启动了服务器,它确实不解决了问题(我发了相反的帖子,但我错了)。
答案 0 :(得分:4)
试试这个。您可以通过命令rake routes
访问路由名称。由于它来自会话控制器,因此默认路由可能类似new_session_path
。要更改它,您需要使用as: new_name
match '/signin', to: 'sessions#new', via: 'get', as: 'signin'
答案 1 :(得分:0)
尝试: -
get "signin" => "sessions#new", :as => "signin"
resources :sessions
将路线用作 new_session_path 或 signin_path
答案 2 :(得分:0)
请参阅此主题:undefined method `visit' when using RSpec and Capybara in rails
由于Capybara的最新变化,您可能在config.include Capybara:DSL
中没有spec_helper.rb
行,而是将测试放在rspec/features/
内。
答案 3 :(得分:-1)
我错误地重新开始解决问题所以撤回这篇文章。
==编辑我发现了rspec这种不稳定行为的罪魁祸首:rails似乎没有清空测试之间的缓存(在我看来,这是一个可怕的bug)。默认情况下,它无法重新读取文件,因此可能会忽略已发生的更改。我在这里提供了更多详细信息:Rails教程:Rails tutorial: undefined method