【问题标题】:Failure/Error: it { should_not have_selector('div.alert.alert-error') }失败/错误:它 { should_not have_selector('div.alert.alert-error') }
【发布时间】:2014-03-25 16:29:46
【问题描述】:

我正在学习 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 ./spec/requests/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.html.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

【问题讨论】:

  • 可以分享问题中的首页吗?
  • @detskai 你能显示你的布局代码,即app/views/layouts/application.html.erb 吗?
  • @detskai 你也可以分享Home页面吗?
  • @detskai 如果你的 root_path 是 root 'static_pages#home',那么请显示你的 root static_pages 控制器。
  • @SachinSingh 是的,root 'static_pages#home' ,添加了 static_pages_controller

标签: ruby-on-rails ruby rspec capybara railstutorial.org


【解决方案1】:

请确保您的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 不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2021-05-25
    • 2016-04-25
    • 2017-05-12
    • 2022-10-15
    • 2018-11-29
    • 2021-10-22
    相关资源
    最近更新 更多