【问题标题】:Rspec & Capybara: uninitialized constant SessionControllerRspec & Capybara:未初始化的常量 SessionController
【发布时间】:2015-03-24 07:09:05
【问题描述】:

我正在关注本教程的身份验证过程 - 目前在“用户身份验证”部分:

http://larsgebhardt.de/user-authentication-with-ruby-on-rails-rspec-and-capybara/

我收到了follower测试失败:

  1) User Management User log in
     Failure/Error: login(@writer)
     ActionController::RoutingError:
       uninitialized constant SessionController
     # ./spec/support/user_helper.rb:6:in `login'
     # ./spec/features/users_spec.rb:27:in `block (2 levels) in <top (required)>'

这是失败消息中提到的特定测试和帮助文件,以及其他文件..

spec/features/users_spec.rb

require 'spec_helper'

    background do
      @writer = create(:user, :writer)
    end

   ....

    scenario 'User log in' do
      activate(@writer)
      login(@writer)
      expect(page).to have_content "Successfully logged in."
    end

spec/support/user_helper.rb

module UserHelper


  def login(a)
    visit root_path
    click_link 'Log In'
    fill_in 'session[email]', with: a.email
    fill_in 'session[password]', with: a.password
    click_button 'Log In'
  end

  def logout(a)
    visit root_path
    click_link 'Log Out'
  end

  def activate(a)
    visit activate_path(:code => a.activation_code)
  end

end

routes.rb

resources :session

sessions_controller.rb

class SessionsController < ApplicationController


  def new
  end

  def create
    user = User.find_by_email(params[:session][:email]).try(:authenticate, params[:session][:password])
    if user
        if user.is_active?
            session[:user_id] = user.id
            redirect_to (session[:target_url] || root_path)
                flash[:notice] = "Successfully logged in."
            else
                redirect_to new_session_path
                flash[:error] = "Account inactive. Please activate your account."
            end
        else
            redirect_to new_session_path
            flash[:error] = "Invalid email or password."
        end
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_path
    flash[:notice] = "Successfully logged out."
  end

end

new.html.erb

<h1>Log In</h1>

<%= form_for :session, url: sessions_path do |f| %>

    <div>
        <%= f.label :email %>
        <%= f.text_field :email %>
    </div>

    <div>
        <%= f.label :password %>
        <%= f.password_field :password %>
    </div>

    <div>
        <%= f.submit 'Log In' %>
    </div>

<% end %>

rake 路线

    Prefix Verb   URI Pattern                                 Controller#Action
    profiles_show GET    /profiles/show(.:format)                    profiles#show
     sessions_new GET    /sessions/new(.:format)                     sessions#new
        users_new GET    /users/new(.:format)                        users#new
             root GET    /                                           sessions#new
    post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                  POST   /posts/:post_id/comments(.:format)          comments#create
 new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
     post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                  PATCH  /posts/:post_id/comments/:id(.:format)      comments#update
                  PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                  DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
            posts GET    /posts(.:format)                            posts#index
                  POST   /posts(.:format)                            posts#create
         new_post GET    /posts/new(.:format)                        posts#new
        edit_post GET    /posts/:id/edit(.:format)                   posts#edit
             post GET    /posts/:id(.:format)                        posts#show
                  PATCH  /posts/:id(.:format)                        posts#update
                  PUT    /posts/:id(.:format)                        posts#update
                  DELETE /posts/:id(.:format)                        posts#destroy
         sessions GET    /sessions(.:format)                         sessions#index
                  POST   /sessions(.:format)                         sessions#create
      new_session GET    /sessions/new(.:format)                     sessions#new
     edit_session GET    /sessions/:id/edit(.:format)                sessions#edit
          session GET    /sessions/:id(.:format)                     sessions#show
                  PATCH  /sessions/:id(.:format)                     sessions#update
                  PUT    /sessions/:id(.:format)                     sessions#update
                  DELETE /sessions/:id(.:format)                     sessions#destroy
            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
    profile_index GET    /profile(.:format)                          profile#index
                  POST   /profile(.:format)                          profile#create
      new_profile GET    /profile/new(.:format)                      profile#new
     edit_profile GET    /profile/:id/edit(.:format)                 profile#edit
          profile GET    /profile/:id(.:format)                      profile#show
                  PATCH  /profile/:id(.:format)                      profile#update
                  PUT    /profile/:id(.:format)                      profile#update
                  DELETE /profile/:id(.:format)                      profile#destroy
         activate GET    /activate/:code(.:format)                   users#activate

_header.html.erb

<a href="#">About</a>
<a href="#">Services</a>
<a href="#"><%= link_to "Sign Up", new_user_path %></a>
<a href="#"><%= link_to "Log In", new_session_path %></a>
<a href="#"><%= link_to "Log Out", "/session", method: :delete %></a>

我之前在配置 User Helper 文件时遇到了问题,但如果它变得相关,您可以在此处查看配置文件的答案和状态:

Rspec email_spec issue

【问题讨论】:

    标签: ruby-on-rails authentication rspec capybara


    【解决方案1】:

    您需要resources :sessions,您缺少“s”。

    编辑:

    你错误地使用了删除路由,你应该说

    <%= link_to "Log Out",session_path(pass the current signed in user here) , method: :delete %>
    

    【讨论】:

    • 感谢您的回复。我仍然收到此错误Failure/Error: login(@writer) ActionView::Template::Error: No route matches {:action=&gt;"show", :controller=&gt;"sessions"} missing required keys: [:id]。让我感到困惑的是,我认为我不需要“展示”动作,这就是为什么假设作者将路线“会话”保持为单数的原因。
    • @Shroy 你定义了根吗?你需要 root "sessions#new" 在你的 routes.rb
    • 是的,我定义的根是posts#index。这是一个博客,我正在尝试创建一个具有“管理员”、writersvisitors 只能发表评论的身份验证系统。手册根页面显示所有帖子。
    • @Shroy 请将新的完整错误添加到问题中。
    • 现在可以登录了!现在注销路线有问题。 2) User Management User log out Failure/Error: logout(@writer) ActionController::RoutingError: No route matches [DELETE] "/session"
    猜你喜欢
    • 2015-06-14
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2019-11-11
    相关资源
    最近更新 更多