Ruby on rails - Failure/Error: it { should_not have_selector('div.alert.alert-error') }



我正在学习RoR教程。当所有的rspec测试都应该变成绿色时,我就是无法让我的工作。我运行bundle exec rspec spec/,在四个测试中,有一个失败了,出现如下错误:

失败:

1)访问后使用无效信息进行认证登录另一个页面

 Failure/Error: it { should_not have_selector('div.alert.alert-error') }
   expected #has_selector?("div.alert.alert-error") to return false, got true
 # ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'Finished in 7.2 seconds 62 examples, 1
失败

失败的例子:

rspec。/规范/要求/authentication_pages_spec。rb:25 #认证访问其他页面后使用无效信息登录

我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('Settings',    href: edit_user_path(user)) }
      it { should have_link('Sign out',    href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }

    describe "follower by signout" do
      before {click_link "Sign out"}
      it {should have_link('Sign in')}
      end
    end
  end
 describe "authorization" do
    describe "for non-signed-in users" do
      let(:user) { FactoryGirl.create(:user) }
      describe "in the Users controller" do
        describe "visiting the edit page" do
          before { visit edit_user_path(user) }
          it { should have_title('Sign in') }
        end
        describe "submitting to the update action" do
          before { patch user_path(user) }
          specify { expect(response).to redirect_to(signin_path) }
        end
      end
    end
  end
 end

application.html。erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag "application", media: "all",
                                           "data-turbolinks-track" => true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <% flash.each do |key, value| %>
        <%= content_tag(:div, value, class: "alert alert-#{key}") %>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>

home。erb:

<div class="center hero-unit">
  <h1>Welcome to the Sample App</h1>
  <h2>
    This is the home page for the
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </h2>
  <%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>

static_pages_controller.rb:

class StaticPagesController < ApplicationController
  def home
  end
  def help
  end
  def about
  end
  def contact
  end
end

请确保您的sessions_controller.rb具有这样定义的create动作:

def create      
    user = User.find_by(email: params[:email].downcase)
    if user && user.authenticate(params[:password])
        # If the user exists and they give a correct password,
        # sign the user in and redirect to the user's show page.
        sign_in user
        redirect_to user 
    else
        # Create an error message and re-render the signin form.
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
    end
end

注意flash.now。它与我们在本教程的其他部分中使用的flash不同。

相关内容

最新更新