0

我正在学习 RoR 教程。在第 8.1.5 章的最后一部分,当所有的 rspec 测试都应该变成绿色时,我就是无法让我的工作。我运行 bundle exec rspec spec/requests/authentication_pages_spec.rb -e "signin with invalid information" 并且在四个测试中,一个失败,出现以下错误:

1) Authentication signin with invalid information after visiting another page 
 Failure/Error: it { should_not have_selector('div.alert.alert-error') }
   expected css "div.alert.alert-error" not to return anything
 # ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'

app/controllers/sessions_controller.rb 的文本如下:

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    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
  end
end

spec/requests/authetication_pages_spec.rb 的文本如下:

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_selector('title', text: '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.alert-error') }
      end
    end 

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before do
        fill_in "Email",    with: user.email
        fill_in "Password", with: user.password
        click_button "Sign in"
      end 

      it { should have_selector('title', text: 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

如果您有任何想法或想法,请告诉我,我完全被难住了!

4

2 回答 2

2

我克隆了您的仓库并发现了您的问题。

头文件(app/views/layouts/_header.html.erb)中的链接不会去任何地方:

<li><%= link_to "Home",    '#' %></li>
<li><%= link_to "Help",    '#' %></li>

因此,当您的测试运行时,以下测试通过,因为显示错误...

describe "with invalid information" do
  before { click_button "Sign in" }

  it { should have_selector('title', text: 'Sign in') }
  it { should have_selector('div.alert.alert-error', text: 'Invalid') }

...但是当您单击一个不会去任何地方的链接时...

  describe "after visiting another page" do
    before { click_link "Home" } # goes nowhere
    it { should_not have_selector('div.alert.alert-error') }
  end
end

...该错误仍在页面上,因此测试正确失败。

"Home"链接目标从更改'#'为 后root_path,您的测试应该会通过。

看文字,如果你在第 8 章,似乎你在第 5.3.3章中的清单 5.2.4上错过了几章,所以一旦你回去修复,你应该处于更好的状态它。

我还建议安装launchygem,以便您可以调用有用的 Capybara 方法,例如save_and_open_page在您的测试中,它会为您打开一个浏览器,显示您调用该方法时的页面状态;对调试有很大帮助。

于 2012-07-17T22:34:53.347 回答
0

代替

it { should_not have_selector('div.alert.alert-error') }

经过

it { should_not have_selector('div.alert.alert-error', text: 'Invalid') }
于 2012-07-12T11:09:33.003 回答