2017-02-12 65 views
0

運作良好,我有以下特性測試合格:Poltergeits不RSpec的測試

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寶石測試javascritp,在我的RSpec rails_helper.rb文件配置的:

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

PhantomJS已經instaled和我的$ PATH可用。我完全沒有想法,這裏會發生什麼?

回答

1

這裏有一些潛在的問題。

  1. 如果已設置Capybara.server = :puma,請確保你沒有運行PUMA 3.7.0其中有一個bug,當3.7.1發佈,這將是固定的。現在使用3.6.9

  2. 如果這是您第一次設置js測試,請確保您已設置並正確配置了DatabaseCleaner,以便您在js: true測試中沒有以事務模式運行。見https://github.com/teamcapybara/capybara#transactions-and-database-setuphttps://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example的推薦配置

  3. 一旦你#1和#2制定了,那麼你就需要看你的測試和處理,一個點擊/ 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. 
    

沒有類似的東西變化檢查將失敗,因爲它會前的實際實際發生檢查計數。

  • 默認爲_path助手而非_url助手時沒有必要對特定的主機名(時間99.9%)。這可以讓Capybara爲正在運行的服務器填充正確的主機/端口。要訪問_url助手,需要仔細設置一些配置參數。
  • 注意:檢查功能測試中的數據庫計數通常不是最佳做法,它們通常應限於檢查可見頁面更改。

    +0

    感謝提示@ThomasWalpole! 1)這裏不是這種情況(我沒有手動設置); 2)我正在使用推薦的配置; 3)即使我刪除了我的計數測試,其他預期失敗(錯誤也是一樣的)。我在所有測試中都遇到過這個問題:'js:true'。我在具體的問題中顯示的這些測試並不使用assynchronous請求,但即使如此,如果存在'js:true',它們也應該通過,對嗎?事實是,只有在我的問題中引用的HTML被加載到這些測試中,並且我沒有嘗試改變這一點。 – rwehresmann

    +1

    wjen我說異步我不是指XHR,我的意思是在測試繼續時發生的動作。當使用JS:true時,無論他們是否進行XHR請求,每次訪問/點擊都可能是異步的。還有一件事是用_path替換你的'訪問xxx_url'。在測試中使用_url助手不是一個好主意,因爲很難正確配置服務器名稱和端口設置 –

    +0

    'visith xxx_path'完成了這個訣竅。請用這個小細節更新你的答案,我會接受它的。非常感謝! – rwehresmann