身份验证登录有效信息

时间:2014-06-28 23:11:52

标签: ruby-on-rails testing railstutorial.org

我正在研究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

[编辑:]这是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

如何让测试通过?

感谢。

1 个答案:

答案 0 :(得分:0)

如果没有看到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,或者根据RESTful {{3}向用户路径发出GET请求},将返回用户/显示视图。如果无法保存,控制器将呈现新视图(app / views / sessions / new.html.erb)。

在没有看到您的控制器的情况下,我怀疑您无法将控制器重定向到另一个操作或渲染另一个视图(因为它大肆宣传缺少创建模板)。

这里的routes表示相同。