使用有效信息进行身份验证登录



我正在学习Michael Hartl的Rails教程,章节8.2.4:http://www.railstutorial.org/book/sign_in_out#sec-changing_the_layout_links

到目前为止,一切似乎都很顺利。

但是,当我在本节末尾运行测试时,教程说它们应该通过,但我得到以下错误:

FFFF.....*....................................
Pending:
  SessionsHelper add some examples to (or delete) /Users/Thibaud/work/rails_projects/sample_app/spec/helpers/sessions_helper_spec.rb
    # No reason given
    # ./spec/helpers/sessions_helper_spec.rb:14
Failures:
  1) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
  2) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
  3) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
  4) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
Finished in 2.04 seconds
46 examples, 4 failures, 1 pending
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:40 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:38 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:37 # Authentication signin with valid information 
Randomized with seed 59639
以下是我的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
    describe "signin" do
        before { visit signin_path }
        describe "with invalid information" do
            before { click_button "Sign in" }
            it { should have_title('Sign in') }
            it { should have_selector('div.alert.alert-error') }
            describe "after visiting another page" do
                before { click_link "Home" }
                it { should_not have_selector('div.alert.alert-error') }
            end
        end
        describe "with valid information" do
            let(:user) { FactoryGirl.create(:user) }
            before do
                fill_in "Email",    with: user.email.upcase
                fill_in "Password", with: user.password
                click_button "Sign in"
            end
            it { should have_title(user.name) }
            it { should have_link('Profile',     href: user_path(user)) }
            it { should have_link('Sign out',    href: signout_path) }
            it { should_not have_link('Sign in', href: signin_path) }
        end
    end
end

[EDIT:]这是SessionController文件:

class SessionsController < ApplicationController
  def new
  end
  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      # Sign the user in and redirect to the user's show page.
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end
  def destroy
    sign_out
    redirect_to root_url
  end
end

我怎样才能使测试通过?

谢谢。

如果没有看到SessionController类的代码,就很难准确地告诉您哪里出错了。在本教程结束时,SessionController中的create action/方法应该如下所示:

def create
  @user = User.new(user_params)
  if @user.save
    sign_in @user
    flash[:success] = "Welcome to the Sample App!"
    redirect_to @user
  else
    render 'new'
  end
end

在此代码中,如果@user正确保存,则代码的redirect_to @user部分告诉控制器重定向到@user,或者向用户路径发出GET请求,根据RESTful路由,该请求将返回user/show视图。如果保存失败,控制器就会渲染新视图(app/views/sessions/new.html.erb)。

没有看到你的控制器,我怀疑你是失败的重定向控制器到另一个动作或渲染另一个视图(因为它大喊大叫关于缺少一个模板创建)。

这是另一个关于SO的答案,表示相同。

最新更新