为什么不能通过这个Rspec测试?

时间:2014-09-11 15:42:22

标签: ruby-on-rails rspec

我正在关注Michael Hartl的ROR教程。我被困在测试authentication_pages。我已经用这本书检查了几次。那里似乎没有错,我现在不知道这个问题。让我详细说明一下。对你的帮助表示感谢!

$ bundle exec rspec spec/

1) AuthenticationPages 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/snailwalker/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:28:in `block (4 levels) in <top (required)>'

Finished in 0.8486 seconds
44 examples, 4 failures

应用/控制器/ sessions_controller.rb

class SessionsController < ApplicationController

      def new
      end

      def create
        user = User.find_by(email: params[:session][:email].downcase)
        if user && user.authenticate(params[:session][:password])
        else
          flash.now[:error] = 'Invalid email/password combination'
          render 'new'
        end
      end

      def destroy
      end
    end

规格/请求/ authentication_pages_spec.rb

require 'spec_helper'

describe "AuthenticationPages" 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', text: 'Invalid') }

        describe "after visiting another page" do
            before { click_link "Home" }
            it { should_not have_selector('div.alert.alter-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

app / views / sessions / new.html.erb

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
    <div class="span6 offset3">
        <%= form_for(:session, url: sessions_path) do |f| %>
        <%= f.label :email %>
        <%= f.text_field :email %>

        <%= f.label :password %>
        <%= f.password_field :password %>
        <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
        <% end %>
        <p>New user? <%= link_to "Sign up now!", signup_path %></p>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

您必须在if user && user.authenticate(params[:session][:password])else之间执行某些操作。

我想重定向?