Rspec的&安培;水豚:未初始化不断SessionController [英] Rspec & Capybara: uninitialized constant SessionController

查看:184
本文介绍了Rspec的&安培;水豚:未初始化不断SessionController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我沿着这个教程的验证过程如下 - 目前部分用户身份验证:

I am following along this tutorial's authentication process - currently on the section 'User Authentication':

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

我收到的追随者测试失败:

I received the follower test failure:

  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)>'

下面是失败消息的具体测试和帮助文件提到,连同其他文件。

Here is the specific test and helper file mention in the failure message, along with other files..

规格/特征/ 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

规格/支持/ 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 %>

耙路线

    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>

我previously有一个问题与配置这是解决了用户辅助文件,但柜面它成为相关的,你可以看到配置文件在这里的答案,状态:

I previously had an issue with configuring the User Helper file which was solved but incase it becomes relevant, you can see the answer and state of the config file here:

Rspec的email_spec问题

推荐答案

您需要资源:会话,你缺少的's'。

You need resources :sessions, you are missing the 's'.

编辑:

您没有正确使用delete路线,你应该说

You are using the delete route incorrectly, you should say

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

这篇关于Rspec的&安培;水豚:未初始化不断SessionController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆