Ruby on Rails教程第8.2.6章未找到Capybara元素错误+没有方法错误未定义方法它

时间:2013-09-03 11:56:00

标签: rspec ruby-on-rails-3.2 capybara

我正在完成本教程并得到此错误

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

任何有关错误以及如何解决问题的帮助和解释都将非常感激。

EDIT。

水豚错误消失但现在我有了一个新错误,

故障:

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

这里有什么问题?我的语法是正确的,并且该行之前没有给出错误,为什么现在呢?任何帮助和解释为什么会发生这种情况将不胜感激。

1 个答案:

答案 0 :(得分:0)

您尚未登录。检查该失败的describe子句的嵌套/缩进,您会发现您已经过早地使用end阻止了“有效信息”阻止it单行。