2017-01-10 51 views
0

我遇到奇怪的非常測試行爲,登錄狀態處理不一致。爲什麼這些測試在同時運行時失敗,但每個都單獨通過?

該規範會記錄用戶,訪問(嵌套或非嵌套)索引頁,並檢查是否顯示正確的內容。記錄是異步提取的,但我認爲這不會產生影響。

當每個規格單獨運行時,它們都通過。當所有規格一起運行時,它們會因爲預期內容丟失而失敗。使用save_and_open_page顯示這是因爲正在呈現登錄頁面,而不是預期的索引頁面。

爲什麼rspec認爲當所有規格一起運行時用戶沒有登錄,但每個規格都單獨傳遞?

測試看起來像所有需要JavaScript本

let(:user) {create :user} 
let(:team) {create :team} 
let(:country) {create :country} 

before :each do 
    login_as(user, scope: :user) 
end 

describe 'unnested' do 
    it 'should have the expected content', :js do 
    visit users_path 
    is_expected.to have_content "some content on the page" 
    end 
end 

describe 'nested by team' do 
    it 'should have the expected content', :js do 
    visit team_users_path(team) 
    is_expected.to have_content "some content on the page" 
    end 
end 

describe 'nested by nationality' do 
    it 'should have the expected content', :js do 
    visit country_users_path(country) 
    is_expected.to have_content "some content on the page" 
    end 
end 

的規格(我不知道這是否是重要的在這裏)。

認證是由設計處理,我rails_helper.rb包括

config.append_after(:each) do 
    DatabaseCleaner.clean 
    Warden.test_reset! 
end 

爲什麼RSpec的認爲用戶不會在所有規格一起運行在簽訂,但每個單獨的規格經過?

回答

0

這需要很長時間才能完成。張貼這聽到的情況下,以幫助其他人遇到同樣的問題。

經過一番搜索我終於找到this small mentionlogin_as may not work with Poltergeist when js is enabled on your test scenarios.

我想建議的修復處理共享數據庫連接。不幸的是這導致了以下錯誤:

PG::DuplicatePstatement at /session/users/signin 
ERROR: prepared statement "a1" already exists 

我嘗試使用Transactional Capybara寶石,但是這似乎並沒有與鬼驅人很好地工作。

最終我完全放棄了login_as,而是寫了一個簡短的方法,訪問登錄頁面,填寫電子郵件和密碼,然後以這種方式登錄。

此解決方案似乎正在工作。它增加了一點開銷,所以我只用它來測試JS。

0

如果您使用的水豚寶石那麼就沒有必要使用:測試用例JS

我做什麼,如果你能使用的功能規格登錄用戶這個helps-

scenario "visit with user signed in" do 
    user = FactoryGirl.create(:user) 
    login_as(user, :scope => :user) 
    visit "/" 
    expect(current_path).to eq('/') 
    expect(page).to have_title "Some Random Title" 
end 

另一種方法喜歡 -

feature 'User signs in' do 
    before :each do 
    @user = FactoryGirl.create(:user) 
    end 

    scenario "Signing in with correct credentials" do 
    visit "/" 
    fill_in "Email", with: @user.email 
    fill_in "Password", with: @user.password 
    click_button "Log In" 
    expect(current_path).to eq("/login/useremail/verification") 
    expect(page).to have_content "Signed in successfully" 
    end 
end 

如果您的網頁阿賈克斯然後參考https://robots.thoughtbot.com/automatically-wait-for-ajax-with-capybara

相關問題