关于Ruby on Rails教程第8章,我得到“失败/错误:fill_in”密码“,用:user.password Capybara :: ElementNotFound:”

时间:2013-05-30 08:02:58

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

由于某种原因,rspec /工厂女孩找不到该邮件。我已经玩了好几个小时,仍然无法让它工作。我用谷歌搜索了它,但它只是不想工作。

require 'spec_helper'

describe "Authentication" do

  subject { page } 

  describe "sign 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', text: 'Invalid') }
    end

    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('Sign out',    href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
        end
    end

以下是会话页面代码:

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>


Controller Code: class UsersController < ApplicationController
  def new
    @user = User.new
  end
  def show
    @user = User.find(params[:id])      
  end
  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end
end

即将出现的错误是:

1) Authentication with valid information
     Failure/Error: fill_in "Email",    with: user.email.upcase
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/requests/authentication_pages_spec.rb:33:in `block (3 levels) in <top (required)>'

非常感谢你的帮助!

3 个答案:

答案 0 :(得分:2)

我在同一个地方遇到了同样的错误。只需在visit signin_path上方添加fill_in "Email", with: user.email.upcase即可。据我理解,创建Factory用户并不意味着您自动指向登录页面。

或者,您可以在创建用户后将该行添加到Factory文件中。

答案 1 :(得分:1)

使用例如Firebug检查生成的html,并检查输入ID。 然后使用带有fill_in的输入ID,在我的例子中是“user_email”和“user_password”

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

答案 2 :(得分:1)

尝试更改

fill_in "Email" to
fill_in "email"

根据您的电子邮件text_field。

如果你将login_email作为文本字段,请在控制台中检查它,看看它是email还是login_email。现在

 fill_in "login_email"