0

RoR チュートリアルを受講しています。チャプター 8.1.5 の最後の部分で、すべての rspec テストが緑色に変わるはずなのに、私のテストが動かなくなってしまいました。bundle exec rspec spec/requests/authentication_pages_spec.rb -e "signin with invalid information" を実行すると、4 つのテストのうち 1 つが失敗し、次のエラーが表示されます。

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.2.4第 5.3.3章の数章前のステップを見逃しているようです。それ。

launchyまた、テストのように便利な Capybara メソッドを呼び出すことができるように、gemをインストールすることをお勧めします。メソッドを呼び出し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 に答える