2017-07-27 92 views
0

使用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_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 

規格/ 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

的錯誤是這裏的很多,所以我們就從頭開始

  1. 你不必在你的spec_helper.rb或rails_helper.rb require capybara/rails - https://github.com/teamcapybara/capybara#setup - 這意味着Capybara.app不獲得設置,這就是爲什麼你要進行「機架測試需要機架應用」 - 當然,你並不是真的想要使用rack-test驅動程序來進行當前的#3測試。

  2. 水豚已經包括Capybara :: DSL功能規格,所以使用功能規格,並從您上面顯示的RSpec配置中刪除include Capybara::DSL。這要求把你的spec文件到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. 當指定使用你需要在你的道路上安裝PhantomJS(Poltergeist要求)。如果使用OSX和自制軟件,你可以做brew install phantomjs - 在其他系統上,你需要下載最新版本的PhantomJS,並把它放在你的路徑中的某個地方

  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)

相關問題