2017-01-10 48 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

相关问题