Poltergeist 在 rspec 测试中表现不佳

Poltergeits doesn't working well in rspec tests

我通过了以下功能测试:

require 'rails_helper'

describe "Create a quetsion", type: :feature do
  let(:question) { build(:question) }
  before do
    login_as create(:user, :teacher)
    exercise = create(:exercise)
    visit new_exercise_question_url(exercise.id)
  end

  context "whit valid attributes" do
    subject do
      fill_in "question_score", with: question.score
      fill_in "question_description", with: question.description
      click_on "Criar"
    end

    it "create the question" do
      expect{ subject }.to change(Question, :count).by(1)
      expect(page).to have_current_path(question_path(Question.first.id))
    end
  end

  context "whit invalid attributes" do
    subject do
      click_on "Criar"
    end

    it "doesn't create the exercise" do
      expect{ subject }.to change(Question, :count).by(0)
      expect(page).to have_selector("div.alert.alert-danger")
    end
  end
end

这很好,除非我添加 js: true。在这种情况下,我有以下错误:

  1) Create a quetsion whit valid attributes create the question
     Failure/Error: fill_in "question_score", with: question.score

     Capybara::ElementNotFound:
       Unable to find field "question_score"
     # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/finders.rb:33:in `find'
     # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/actions.rb:85:in `fill_in'
     # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:735:in `block (2 levels) in <class:Session>'
     # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/create_a_question_spec.rb:14:in `block (3 levels) in <top (required)>'
     # ./spec/features/create_a_question_spec.rb:20:in `block (4 levels) in <top (required)>'
     # ./spec/features/create_a_question_spec.rb:20:in `block (3 levels) in <top (required)>'

  2) Create a quetsion whit invalid attributes doesn't create the exercise
     Got 0 failures and 2 other errors:

     2.1) Failure/Error: visit new_exercise_question_url(exercise.id)

          Capybara::Poltergeist::StatusFailError:
            Request to 'http://www.example.com/exercises/1/questions/new' failed to reach server, check DNS and/or server status
          # /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:376:in `command'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:35:in `visit'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/driver.rb:97:in `visit'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:240:in `visit'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
          # ./spec/features/create_a_question_spec.rb:8:in `block (2 levels) in <top (required)>'

     2.2) Failure/Error: @socket.send(command.id, command.message, receive_timeout) or raise DeadClient.new(command.message)

          Capybara::Poltergeist::DeadClient:
            PhantomJS client died while processing {"id":"2928bf26-2dd8-45d7-8822-41b2923fd40d","name":"reset","args":[]}
          # /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/server.rb:38:in `send'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:369:in `command'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:224:in `reset'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/driver.rb:183:in `reset!'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:109:in `reset!'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `block in reset_sessions!'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reverse_each'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reset_sessions!'
          # /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rspec.rb:21:in `block (2 levels) in <top (required)>'

我检查了一下,js: true 我的页面 html 也没有写(而是我想要的 HTML,我只收到 <html><head></head><body></body></html>)。

我添加了 poltergeist gem 来测试 javascritp,并在我的 rspec rails_helper.rb 文件中配置了它:

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

PhantomJS 已经安装并在我的 $PATH 中可用。我完全没有想法,这里会发生什么?

这里有很多潜在的问题。

  1. 如果你设置了 Capybara.server = :puma,确保你不是 运行ning puma 3.7.0,它有一个错误,将在 3.7.1 时修复被释放。现在使用 3.6.9

  2. 如果这是您第一次设置 js 测试,请确保您已经设置并正确配置了 DatabaseCleaner,这样您就不会 运行 处于 [=12] 的事务模式=] 测试。推荐配置见https://github.com/teamcapybara/capybara#transactions-and-database-setup and https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example

  3. 一旦您完成了#1 和#2,那么您需要查看您的测试并处理在 click/click_on 类型操作之后您需要检查的事实可见的页面更改,以确保在您继续之前操作已完成。这是因为点击会发生,但这些点击触发的操作可以异步发生。在您当前的示例中,这意味着您需要

    ...
    click_on "Criar"
    expect(page).to have_text("Question created!") #whatever message is shown when the action has completed, or check the page etc.
    

如果没有类似的东西,更改检查将失败,因为它会在实际发生之前检查计数。

  1. 当不需要特定主机名时(99.9% 的时间),默认为 _path 个助手而不是 _url 个助手。这让 Capybara fill_in 正确的 host/port 服务器 运行。要访问 _url 帮助程序,需要仔细设置许多配置参数。

注意:在功能测试中检查数据库计数通常不是最佳做法,它们通常应仅限于检查可见页面更改。