Puffing Billy with Poltergeist error: "rack-test requires a rack application, but none was given"

Puffing Billy with Poltergeist error: "rack-test requires a rack application, but none was given"

使用 Puffing Billy instructions for rspec with capybara 我创建了一个简单的测试来存根使用 :poltergeist_billy 驱动程序的请求导致错误:

ArgumentError:
        rack-test requires a rack application, but none was given
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara/rack_test/driver.rb:16:in `initialize'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara.rb:372:in `new'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara.rb:372:in `block in <top (required)>'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `driver'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara/session.rb:227:in `visit'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
      # ./spec/scraypa_spec.rb:52:in `block (4 levels) in <top (required)>'

使用此代码:

spec/spec_helper.rb

require "bundler/setup"
require "scraypa"
require 'billy/capybara/rspec'

RSpec.configure do |config|
  # Enable flags like --only-failures and --next-failure
  config.example_status_persistence_file_path = ".rspec_status"

  config.expect_with :rspec do |c|
    c.syntax = :expect
  end

  config.include Capybara::DSL
end

spec/my_spec.rb:

it "should utilise capybara to download web content" do
  #Capybara.current_driver = :poltergeist_billy
  Capybara.javascript_driver = :poltergeist_billy
  proxy.stub('http://www.google.com/')
        .and_return(:text => "test response")
  visit "http://www.google.com/"
  expect(page.text).to eq('test response')
end

在四处挖掘时,我发现了一个使用 Capybara.current_driver = :poltergeist_billy 的示例(我在上面的测试中已将其注释掉),如果我取消注释该代码,则会收到此错误:

Cliver::Dependency::NotFound:
        Could not find an executable ["phantomjs"] on your path.
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/cliver-0.3.2/lib/cliver/dependency.rb:143:in `raise_not_found!'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/cliver-0.3.2/lib/cliver/dependency.rb:116:in `detect!'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/cliver-0.3.2/lib/cliver.rb:24:in `detect!'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.9.0/lib/capybara/poltergeist/client.rb:36:in `initialize'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.9.0/lib/capybara/poltergeist/client.rb:14:in `new'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.9.0/lib/capybara/poltergeist/client.rb:14:in `start'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.9.0/lib/capybara/poltergeist/driver.rb:42:in `client'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.9.0/lib/capybara/poltergeist/driver.rb:25:in `browser'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.9.0/lib/capybara/poltergeist/driver.rb:95:in `visit'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara/session.rb:227:in `visit'
      # /home/resrev/.rvm/gems/ruby-2.3.1/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
      # ./spec/scraypa_spec.rb:52:in `block (4 levels) in <top (required)>'

我不确定从这里到哪里去或我做错了什么,有什么想法吗?谢谢。

这里错误较多,还是从头开始吧

  1. 您的 spec_helper.rb 或 rails_helper.rb - https://github.com/teamcapybara/capybara#setup 中没有 require capybara/rails - 这意味着 Capybara.app 不是得到设置,这就是为什么你得到 "rack-test requires a rack application" - 当然你真的不想在你当前的测试中使用 rack-test 驱动程序,#3 将处理。

  2. Capybara 已将 Capybara::DSL 包含在功能规范中,因此请使用功能规范,并从上面显示的 RSpec 配置中删除 include Capybara::DSL。这需要将您的规范文件放入 spec/features/my_spec.rb 并启用 RSpec 配置以按目录确定测试类型,或者手动指定测试是功能规范

    feature "should utilise capybara to download web content" do
      ...
    end
    

    it "should utilise capybara to download web content", type: :feature do
      ...
    end
    
  3. 您的测试实际上使用的是 rack_test 驱动程序,而不是 poltergeist-billy 驱动程序。这是因为您在测试中设置 Capybara.javascript_driver 。它需要在测试之前设置,然后用元数据标记测试以告诉它使用特定的驱动程序。这里有两个选项,要么在 spec_helper.rb 中设置 Capybara.javascript_driver = :poltergeist_billy,然后指定 :js metadata

     feature "should utilise capybara to download web content", :js do
       ...
     end
    

    或指定 :driver 元数据以识别用于给定测试的驱动程序

    feature "should utilise capybara to download web content", driver: :poltergeist_billy do 
      ... 
    end
    
  4. 当指定将 poltergeist 与 billy 一起使用时,您需要在路径中安装 PhantomJS(Poltergeist 需要)。如果将 OSX 与自制软件一起使用,您可以 brew install phantomjs - 在其他系统上,您需要下载最新版本的 PhantomJS 并将其放在 PATH

  5. 的某个位置
  6. expect(page.text).to eq('test response')。这是使用 Capybara 进行文本匹配的糟糕方法。 eq 没有 waiting/retrying 行为,因为当 Capybaras 方法 return 会导致不稳定的测试时,操作无法保证完成。相反,请使用 Capybara 提供的匹配器。如果您对子字符串匹配没问题,请执行

    expect(page).to have_text('test_response')

    如果需要完全匹配

    expect(page).to have_text('test_response', exact: true)