【问题标题】:Puffing Billy with Poltergeist error: "rack-test requires a rack application, but none was given"用 Poltergeist 错误吹嘘比利:“机架测试需要机架应用程序,但没有给出”
【发布时间】:2017-07-28 12:51:46
【问题描述】:

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

我不知道从这里去哪里或者我做错了什么,有什么想法吗?谢谢。

【问题讨论】:

    标签: ruby-on-rails rspec capybara poltergeist


    【解决方案1】:

    这里的错误很多,让我们从头开始

    1. 您的 spec_helper.rb 或 rails_helper.rb 中没有 require capybara/rails - https://github.com/teamcapybara/capybara#setup - 这意味着 Capybara.app 没有设置好,这就是您获得“机架测试”的原因需要一个机架应用程序”——当然,您并不想将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 元数据

       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 与 homebrew 一起使用,您可以执行 brew install phantomjs - 在其他系统上,您需要下载最新版本的 PhantomJS 并将其放在 PATH 中的某个位置

    5. expect(page.text).to eq('test response')。在使用 Capybara 时,这是一种糟糕的文本匹配方式。 eq 没有等待/重试行为,因为当 Capybaras 方法返回时,动作不能保证完成,这将导致不稳定的测试。相反,请使用 Capybara 提供的匹配器。如果你对子字符串匹配没问题

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

      如果需要完全匹配

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

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多