2015-04-30 18 views
3

我正在使用Rails 4,RSpec,水豚和硒。我遇到了一個奇怪的現象,我似乎無法解釋,在最後一天左右嘗試過。我有一個相當基本的請求規格失敗,因爲測試沒有註冊對模型的更改。這是我的規格:Rails 4 Rspec expect {}。儘管測試正在按計劃進行,但沒有註冊

describe "manage pages", :js => true do 
    before :each do 
     @fake_page = FactoryGirl.create(:page_with_block, name: "Foobar") 
     @fake_page2 = FactoryGirl.create(:page_with_block, name: "sdfdf") 
     visit admin_pages_path 
     save_page 
    end 

    it "should delete an image when link is clicked" do 
     expect { 
      within "#page_#{@fake_page.id}" do 
       click_link 'Destroy' 
      end 
      wait = Selenium::WebDriver::Wait.new ignore: Selenium::WebDriver::Error::NoAlertPresentError 
      alert = wait.until { page.driver.browser.switch_to.alert } 
      alert.accept 
     }.to change(Page, :count).by(-1) 
     expect(page).to have_content "Listing Pages" 
     expect(page).not_to have_content "Foobar" 
     save_page   
    end 
end 

正如你可以看到它是一個基本點擊摧毀並確認js警報測試。問題是,它總是失敗:

......F 

Failures: 

    1) Pages manage pages should delete an image when link is clicked 
    Failure/Error: expect { 
     expected #count to have changed by -1, but was changed by 0 
    # ./spec/requests/admin/pages_spec.rb:116:in `block (3 levels) in <top (required)>' 

Finished in 11.74 seconds (files took 3.81 seconds to load) 
7 examples, 1 failure 

按照save_page文件我以前就擺在我,和一個在測試結束時,我註釋掉「expect.to變化」,一切正在按預期工作,虛假頁面在之前被創建,測試通過並且點擊銷燬並接受警報,頁面被銷燬並且索引頁面在沒有內容的情況下被重新加載。我不明白爲什麼變更匹配器沒有看到它,實際上,工作。

我已閱讀,它可能與我的rails_helper.rb文件,但什麼都做我擡頭也有類似的,如果不準確,設置:

ActiveRecord::Migration.maintain_test_schema! 

RSpec.configure do |config| 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    config.use_transactional_fixtures = false 

    config.infer_spec_type_from_file_location! 

    config.include Capybara::DSL 

    config.before(:suite) do 
     DatabaseCleaner.strategy = :truncation 
     DatabaseCleaner.clean_with(:truncation) 
    end 
    config.before(:each) do 
     DatabaseCleaner.strategy = :transaction 
    end 
    config.before(:each, :js => true) do 
     DatabaseCleaner.strategy = :truncation 
    end 
    config.before(:each) do 
     DatabaseCleaner.start 
    end 
    config.after(:each) do 
     DatabaseCleaner.clean 
    end 

    config.after(:suite) do 
     FileUtils.rm_rf(Dir["#{Rails.root}/public/test/system/"]) 
    end 


end 

因爲我能找到的這個沒有其他發生在谷歌或本網站上,我必須在這裏做錯事。任何人都可以指出我的代碼中的缺陷?提前致謝。

+0

這同樣的事情經常發生在我身上。在大多數情況下,我已經(儘可能)通過應用程序本身的UI檢查來查看是否創建或銷燬了任何適當的記錄,然後擺脫該陳述。但是有些地方創建/銷燬的東西幾乎不能通過UI進行測試。你有沒有想到這一點?如果是這樣,我很想知道解決方案。 – tobogranyte

回答

1

我一直在努力解決同樣的問題。你等待邏輯可能有缺陷。我用this approach,它對我很好。

從這篇文章Copypasted - 基本上你需要創建一個幫手:

# spec/support/wait_for_ajax.rb 
module WaitForAjax 
    def wait_for_ajax 
    Timeout.timeout(Capybara.default_max_wait_time) do 
     loop until finished_all_ajax_requests? 
    end 
    end 

    def finished_all_ajax_requests? 
    page.evaluate_script('jQuery.active').zero? 
    end 
end 

RSpec.configure do |config| 
    config.include WaitForAjax, type: :feature 
end 

spec_helper.rb

spec/support/**/*.rb in our spec_helper.rb 

自動包含所有支持文件或做手工,像我這樣的:

#rails_helper.rb 
require_relative 'support/wait_for_ajax' 

現在我的測試通過了!

require 'rails_helper' 

RSpec.feature "Call Me Back contact front feature spec >", :type => :feature, js: true do 
feature 'valid form' do 
    scenario "with name and phone" do 
    visit '/' 
    execute_script '$("#call-popup").fadeIn(300)' 

    fill_in 'call_me_back_request_name', with: 'Clark Kent' 
    fill_in 'call_me_back_request_phone', with: '1234445566' 

    within '#new_call_me_back_request' do 
     expect{ 
     find("input[type='submit']").click 
     wait_for_ajax #<- the magic is here! 
     }.to change(Request, :count).by(1) 
    end 
    end 
end 
相關問題