2

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

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)>'

非常感谢你的帮忙!

4

3 回答 3

2

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

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

于 2013-05-30T09:53:11.057 回答
1

尝试改变

fill_in "Email" to
fill_in "email"

根据您的电子邮件 text_field。

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

 fill_in "login_email"
于 2014-12-03T10:54:58.210 回答
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
于 2013-05-30T08:14:27.813 回答