2017-10-19 94 views
0

我有一个规格,通过当我加js: true它失败。无头Chrome与Rspec数据库连接不工作

它失败,因为visit survey_path(survey)之前有一个surveyaccount然后尝试运行该行visit survey_path(survey),我得到undefined method surveys for nil:NilClass因为没有账户或调查。

这就像所有东西都从数据库中删除。

我有一个视图内的反应组件,所以我想写它的功能规格,因此我需要js: true的原因。

任何人都知道为什么数据库没有调查和帐户,当我有js: true

describe '#edit', js: true do 
    let(:new_survey_name) { 'new survey name' } 

    context 'authenticated user' do 
     before do 
     login_as user 

     # Here there is an account and survey. 
     # Then inside the code when Survey#show is hit there is 
     # no account or survey 
     visit survey_path(survey) 
     end 

     it 'can edit survey' do 
     click_link 'Settings' 
     fill_in 'survey[name]', with: new_survey_name 

     click_button 'Save' 
     visit survey_path(survey) 
     expect(page).to have_content(new_survey_name) 
     expect(page).not_to have_content('Status') 
     end 
    end 

导轨帮助文件有这个里面

RSpec.configure do |config| 
    config.include FactoryGirl::Syntax::Methods 
    config.use_transactional_fixtures = true 
    config.infer_spec_type_from_file_location! 
    config.filter_rails_from_backtrace! 
    config.include RequestSpecHelper, type: :request 

    config.include Warden::Test::Helpers 

    config.use_transactional_fixtures = false 

    config.before(:each) do 
    DatabaseCleaner.strategy = :transaction 
    end 

    config.around(:each) do |example| 
    DatabaseCleaner.cleaning do 
     example.run 
    end 
    end 

    config.before(:suite) do 
    begin 
     DatabaseCleaner.clean_with(:truncation) 
    ensure 
     DatabaseCleaner.clean 
    end 
    end 
end 


Capybara.register_driver :chrome do |app| 
    Capybara::Selenium::Driver.new(app, browser: :chrome) 
end 

Capybara.register_driver :headless_chrome do |app| 
    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { 
     args: %w(headless disable-gpu) 
    } 
) 

    Capybara::Selenium::Driver.new app, 
    browser: :chrome, 
    desired_capabilities: capabilities 
end 

Capybara.configure do |config| 
    config.default_max_wait_time = 5 
    config.javascript_driver = :headless_chrome 
    config.server_host = 'localhost' 
    config.server_port = 54_321 
end 

回答

1

当你运行特性测试,建议使用:truncation清洁方法。只需添加这样的事情你database_cleaner配置:

config.before(:each, type: :feature) do 
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test 

    DatabaseCleaner.strategy = :truncation if !driver_shares_db_connection_with_specs 
end 

来源:https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example