0

チュートリアルを進めていたところ、このエラーが発生しました

Failures:

  1) Authentication signin followed by signout 
     Failure/Error: before { click_link "Sign out" }
     Capybara::ElementNotFound:
      no link with title, id or text 'Sign out' found
     # (eval):2:in `click_link'
     # ./spec/requests/authentication_pages_spec.rb:48:in `block (4 levels) in <top (required)>'

この質問を徹底的に調査しました。同様の質問がありますが、解決策を試しましたが、うまくいきませんでした。

関連するコードは次のとおりです。

セッションコントローラー

class SessionsController < ApplicationController

 def new
 end

  def create
    user = User.find_by_email(params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      # Sign the user in and redirect to the user's show page.
      sign_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end

セッションヘルパー

module SessionHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
  end

  def signed_in?
   !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by_remember_token(remember_token)
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end 
end

ユーザーページの仕様

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "signup page" do
    before { visit signup_path }

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

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_selector('h1',    text: user.name) }
    it { should have_selector('title', text: user.name) }
  end

    describe "signup" do

      before { visit signup_path }

      let(:submit) { "Create my account" }

      describe "with invalid information" do
        it "should not create a user" do
          expect { click_button submit }.not_to change(User, :count)
        end
      end

      describe "with valid information" do
        before do
          fill_in "Name",         with: "Example User"
          fill_in "Email",        with: "user@example.com"
          fill_in "Password",     with: "foobar"
          fill_in "Confirmation", with: "foobar"
        end

        it "should create a user" do
          expect { click_button submit }.to change(User, :count).by(1)
        end

        describe "after saving the user" do
          before { click_button submit }
          #let(:user) { User.find_by(email: 'user@example.com') }
          let(:user) { User.where(email: 'user@example.com').first }

          #it { should have_title(user.name) }
          it { should have_selector( "title", :content => user.name)}
          #it { should have_selector('title', text: user.name) }
          it { should have_selector('div.alert.alert-success', text: 'Welcome') }
          it { should have_link('Sign out') }       
        end
      end
   end
 end

_header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "sample app", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", root_path %></li>
          <li><%= link_to "Help", help_path %></li>
          <% if signed_in? %>
            <li><%= link_to "Users", '#' %></li>
            <li id="fat-menu" class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                Account <b class="caret"></b>
              </a>
              <ul class="dropdown-menu">
                <li><%= link_to "Profile", current_user %></li>
                <li><%= link_to "Settings", '#' %></li>
                <li class="divider"></li>
                <li>
                  <%= link_to "Sign out", signout_path, method: "delete" %>
                </li>
              </ul>
            </li>
          <% else %>
            <li><%= link_to "Sign in", signin_path %></li>
          <% end %>
        </ul>
      </nav>
    </div>
  </div>
</header>

認証ページの仕様

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') }
    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.upcase
       fill_in "Password", with: user.password
       click_button "Sign in"
     end

     #it { should have_title(user.name) }
     it { should have_selector( "title", :content => 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

     describe "followed by signout" do
       before { click_link "Sign out" }
       it { should have_link('Sign in') }
     end
   end
 end

何が間違っているのか、それを解決する方法についての助けと説明をいただければ幸いです。

編集。

カピバラのエラーはなくなりましたが、新しいエラーが発生しました。

失敗:

1) Authentication signin with valid information followed by signout 
   Failure/Error: it { should have_selector( "title", :content => user.name)}
   NoMethodError:
   undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2::Nested_2::Nested_1:0xb4063c4>
 # ./spec/requests/authentication_pages_spec.rb:42:in `block (4 levels) in <top (required)>'

認証ページの仕様の編集版は次のとおりです。

require 'spec_helper'

describe "Authentication" do
.
.
.
.
  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"


    #it { should have_title(user.name) }
    #it { should have_selector('title', text: user.name) }
    it { should have_selector( "title", :content => 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

  describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
   end
  end
 end
end

ここで何が問題なのですか?私の構文は正しく、その行は以前はエラーを出していなかったのに、なぜ今なのか? なぜこれが起こっているのか、助けと説明をいただければ幸いです。

4

1 に答える 1

0

サインインしていません。その失敗した句のネスト/インデントを確認すると、「有効な情報を含む」ブロックを途中で終了し、直後のワンライナーdescribeで終了したことがわかります。endit

于 2013-09-03T13:51:31.607 に答える