Headless Chrome 与 Rspec 数据库连接不工作
Headless Chrome with Rspec database connection not working
我有一个规范通过了 但是 当我添加 js: true
它失败了。
它失败了,因为在 visit survey_path(survey)
之前有一个 survey
和一个 account
然后它尝试 运行 那行 visit survey_path(survey)
我得到 undefined method surveys for nil:NilClass
因为没有帐户或调查。
好像所有的东西都从数据库中删除了。
我在视图中有一个 React 组件,所以我想为它编写功能规范,因此我需要 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
rails 帮助文件里面有这个
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
在运行功能测试时,推荐使用:truncation
clean方法。只需将类似这样的内容添加到您的 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
我有一个规范通过了 但是 当我添加 js: true
它失败了。
它失败了,因为在 visit survey_path(survey)
之前有一个 survey
和一个 account
然后它尝试 运行 那行 visit survey_path(survey)
我得到 undefined method surveys for nil:NilClass
因为没有帐户或调查。
好像所有的东西都从数据库中删除了。
我在视图中有一个 React 组件,所以我想为它编写功能规范,因此我需要 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
rails 帮助文件里面有这个
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
在运行功能测试时,推荐使用:truncation
clean方法。只需将类似这样的内容添加到您的 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