Rspec Capybara::ElementNotFound:无法找到字段“;电邮;铁路结构(第9章)

Rspec Capybara::ElementNotFound:无法找到字段“;电邮;铁路结构(第9章),rspec,ruby-on-rails-4,capybara,factory-bot,railstutorial.org,Rspec,Ruby On Rails 4,Capybara,Factory Bot,Railstutorial.org,我正在学习Railstutorial(第9章) “删除链接”的测试第一次通过,但从下一次开始,它会出现一些错误,如: Failures: 1) User pages index delete links as an admin user Failure/Error: sign_in admin Capybara::ElementNotFound: Unable to find field "Email" # ./spec/support/uti

我正在学习Railstutorial(第9章) “删除链接”的测试第一次通过,但从下一次开始,它会出现一些错误,如:

Failures:

  1) User pages index delete links as an admin user 
     Failure/Error: sign_in admin
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/support/utilities.rb:20:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:38:in `block (5 levels) in <top (required)>'

  2) User pages index delete links as an admin user 
     Failure/Error: sign_in admin
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/support/utilities.rb:20:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:38:in `block (5 levels) in <top (required)>'

  3) User pages index delete links as an admin user should be able to delete another user
     Failure/Error: sign_in admin
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/support/utilities.rb:20:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:38:in `block (5 levels) in <top (required)>'

Finished in 0.81336 seconds
4 examples, 3 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:48 # User pages index delete links as an admin user 
rspec ./spec/requests/user_pages_spec.rb:42 # User pages index delete links as an admin user 
rspec ./spec/requests/user_pages_spec.rb:43 # User pages index delete links as an admin user should be able to delete another user

Randomized with seed 11884
用户页面\u spec.rb文件的一部分,其中错误指示

describe "delete links" do

      it { should_not have_link('delete') }

      describe "as an admin user" do
        let(:admin) { FactoryGirl.create(:admin) }
        before do
          sign_in admin
          visit users_path
        end

        it { should have_link('delete', href: user_path(User.first)) }
        it "should be able to delete another user" do
          expect do
            click_link('delete', match: :first)
          end.to change(User, :count).by(-1)
        end
        it { should_not have_link('delete', href: user_path(admin)) }
      end
    end
  end
存在登录定义的sessions\u helper.rb文件

module SessionsHelper

  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 current_user?(user)
    user == current_user
  end

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

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
  end

  def store_location
    session[:return_to] = request.url if request.get?
  end
end
我甚至尝试过在类似帖子中建议的“email\u id”中填充,但在这种情况下不起作用

谢谢

这里是new.html.erb的内容,html正在为登录呈现该内容

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

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

登录
新用户


这是一个老问题,但我在学习本教程时也遇到了这个问题。把测试搞得一团糟的是,前面示例中的状态正在泄漏到“作为管理员用户”示例中

要查看此操作,您可以在调用登录帮助程序之前,在before块中添加
访问登录路径
,然后
保存并打开页面

describe "as an admin user" do
  let(:admin) { FactoryGirl.create(:admin) }
    before do
      visit signin_path
      save_and_open_page
      sign_in admin
      visit users_path
    end
当我的浏览器呈现页面时,我发现以前登录的用户从未注销,因此访问登录页面只会将您重定向到该用户的配置文件页面,并且用户配置文件页面上没有电子邮件字段。这就是为什么水豚报告
无法找到字段“Email”

为了解决这个问题,我在utilities.rb文件中添加了一个sign_out助手

def sign_out
  first(:link, "Sign Out").click
end
在示例中,我将其称为“在admin中签名”

describe "as an admin user" do
  let(:admin) { FactoryGirl.create(:admin) }
    before do
      sign_out
      sign_in admin
      visit users_path
    end

之后我的所有测试都通过了。

这是一个老问题,但我在学习教程时也遇到了这个问题。把测试搞得一团糟的是,前面示例中的状态正在泄漏到“作为管理员用户”示例中

要查看此操作,您可以在调用登录帮助程序之前,在before块中添加
访问登录路径
,然后
保存并打开页面

describe "as an admin user" do
  let(:admin) { FactoryGirl.create(:admin) }
    before do
      visit signin_path
      save_and_open_page
      sign_in admin
      visit users_path
    end
当我的浏览器呈现页面时,我发现以前登录的用户从未注销,因此访问登录页面只会将您重定向到该用户的配置文件页面,并且用户配置文件页面上没有电子邮件字段。这就是为什么水豚报告
无法找到字段“Email”

为了解决这个问题,我在utilities.rb文件中添加了一个sign_out助手

def sign_out
  first(:link, "Sign Out").click
end
在示例中,我将其称为“在admin中签名”

describe "as an admin user" do
  let(:admin) { FactoryGirl.create(:admin) }
    before do
      sign_out
      sign_in admin
      visit users_path
    end

之后我的所有测试都通过了。

我也有这个问题。不幸的是,我也无法使建议的注销功能正常工作。尽管save_和open_页面(在安装gem“launchy”之后)显示了一个“注销”链接,但测试报告在页面上找不到一个链接。我最终解决了以下测试:

  • 我将“删除链接”块从“索引”块中移出
  • 我在新的“删除链接”块开始时重新创建了30个测试用户
  • 我将非管理员用户页面上缺少删除链接的测试移回“分页”块中的新块
  • (当然,还有两个谜团:1)我不知道为什么代码可以像书中所说的那样工作。那么,为什么阅读这本书的每个人都没有这个问题呢?2) 为什么我的“注销”功能找不到“注销”链接,该链接清楚地显示在“保存”和“打开”页面上。有时候,你必须继续前进才能不断进步。至少现在它可以工作了。)

    下面是进行这些更改后的代码。现在它通过了所有测试:

      describe "index" do
        let(:user) { FactoryGirl.create(:user) }
    
        before(:each) do
          sign_in user
          visit users_path
        end
    
        it { should have_title('All users') }
        it { should have_content('All users') }
    
        describe "pagination" do
    
          before(:all) { 30.times { FactoryGirl.create(:user) } }
          after(:all)  { User.delete_all }
    
          it { should have_selector('div.pagination') }
    
          it "should list each user" do
            User.paginate(page: 1).each do |user|
              expect(page).to have_selector('li', text: user.name)
            end
          end
    
          describe "non-admin user should not have delete links" do
            it { should_not have_link('delete', href: user_path(User.first)) }
          end
        end
      end
    
    
      # fhj: I could not get this to work as done in the book (i.e. placing this
      # above inside the "index" block). It could not execute sign_in admin
      # because it was already signed in as a non-admin user. I tried the
      # suggestion to create a sign_out helper function, but I could not make
      # that work either. By moving it outside of the block and recreating
      # the 30 test users, I was able to get it to pass the tests.
      # I also created the "non-admin user should not have delete links" block
      # at the end of the "pagination" block above to test for that case properly.
      describe "delete links" do
    
        before(:all) { 30.times { FactoryGirl.create(:user) } }
        after(:all)  { User.delete_all }
    
        describe "as an admin user" do
          let(:admin) { FactoryGirl.create(:admin) }
          before do
            sign_in admin
            visit users_path
          end
    
          it { should have_link('delete', href: user_path(User.first)) }
          it "should be able to delete another user" do
            expect do
              click_link('delete', match: :first)
            end.to change(User, :count).by(-1)
          end
          it { should_not have_link('delete', href: user_path(admin)) }
        end
      end
    

    我也有这个问题。不幸的是,我也无法使建议的注销功能正常工作。尽管save_和open_页面(在安装gem“launchy”之后)显示了一个“注销”链接,但测试报告在页面上找不到一个链接。我最终解决了以下测试:

  • 我将“删除链接”块从“索引”块中移出
  • 我在新的“删除链接”块开始时重新创建了30个测试用户
  • 我将非管理员用户页面上缺少删除链接的测试移回“分页”块中的新块
  • (当然,还有两个谜团:1)我不知道为什么代码可以像书中所说的那样工作。那么,为什么阅读这本书的每个人都没有这个问题呢?2) 为什么我的“注销”功能找不到“注销”链接,该链接清楚地显示在“保存”和“打开”页面上。有时候,你必须继续前进才能不断进步。至少现在它可以工作了。)

    下面是进行这些更改后的代码。现在它通过了所有测试:

      describe "index" do
        let(:user) { FactoryGirl.create(:user) }
    
        before(:each) do
          sign_in user
          visit users_path
        end
    
        it { should have_title('All users') }
        it { should have_content('All users') }
    
        describe "pagination" do
    
          before(:all) { 30.times { FactoryGirl.create(:user) } }
          after(:all)  { User.delete_all }
    
          it { should have_selector('div.pagination') }
    
          it "should list each user" do
            User.paginate(page: 1).each do |user|
              expect(page).to have_selector('li', text: user.name)
            end
          end
    
          describe "non-admin user should not have delete links" do
            it { should_not have_link('delete', href: user_path(User.first)) }
          end
        end
      end
    
    
      # fhj: I could not get this to work as done in the book (i.e. placing this
      # above inside the "index" block). It could not execute sign_in admin
      # because it was already signed in as a non-admin user. I tried the
      # suggestion to create a sign_out helper function, but I could not make
      # that work either. By moving it outside of the block and recreating
      # the 30 test users, I was able to get it to pass the tests.
      # I also created the "non-admin user should not have delete links" block
      # at the end of the "pagination" block above to test for that case properly.
      describe "delete links" do
    
        before(:all) { 30.times { FactoryGirl.create(:user) } }
        after(:all)  { User.delete_all }
    
        describe "as an admin user" do
          let(:admin) { FactoryGirl.create(:admin) }
          before do
            sign_in admin
            visit users_path
          end
    
          it { should have_link('delete', href: user_path(User.first)) }
          it "should be able to delete another user" do
            expect do
              click_link('delete', match: :first)
            end.to change(User, :count).by(-1)
          end
          it { should_not have_link('delete', href: user_path(admin)) }
        end
      end
    

    你好,有人能回答这个问题吗?嗨,应用程序运行正常吗?你能登录吗?您的视图代码是什么样子的?是的,应用程序运行得很好,但它是第一次通过的测试,但从下一次开始它就没有通过。你能告诉我一些名字吗?因为视图部分下有很多文件。登录路径的视图。在我看来,为登录字段呈现的html似乎不是电子邮件,因此capybara在视图中找不到它。我已添加了html文件..看一看Hello,有人可以回答此问题吗?嗨,应用程序运行正确吗?你可以登录吗?您的视图代码是什么样子的?是的,应用程序运行得很好,但它是第一次通过的测试,但从下一次开始它就没有通过。你能告诉我一些名字吗?因为视图部分下有很多文件。登录路径的视图。对我来说,为登录字段呈现的html似乎不是电子邮件,因此capybara无法在视图中找到它。我已添加了html文件。请看一看,谢谢,即使在这么多天之后,我仍然无法解决此问题,但感谢您的解释,现在我已经找到了。谢谢,即使过了这么多天,我还是无法解决这个问题,但多亏了你的解释,现在我明白了。