我这个问题已经有一个多星期了。我正在使用Apartment gem进行多租户项目。我希望使用Capybara-WebKit驱动程序测试所有数据库配置设置的JavaScript功能。但是,我无法让它工作,因为我的FacotryGirl实例没有保存。谁能看到这些代码的一部分并提出一些建议?
rails_helper.rb:
RSpec.configure do |config|
.
.
config.use_transactional_fixtures = false
config.add_setting(:seed_tables)
config.before(:suite) do
#DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation, {except: config.seed_tables})
end
# Before each spec run, just to insure consistency between specs
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation, {except: config.seed_tables}
end
config.before(:each) do
DatabaseCleaner.start
end
# After each spec run, just to insure consistency between specs
config.after(:each) do
DatabaseCleaner.clean
Apartment::Tenant.reset
drop_schemas
Capybara.app_host = 'http://example.com'
reset_mailer
end
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
Capybara.configure do |config|
config.app_host = 'http://example.com'
config.javascript_driver = :webkit
config.always_include_port = true
end
Capybara::Webkit.configure do |config|
# Allow a specific domain without issuing a warning.
config.allow_url("lvh.me")
# Wildcards are allowed in URL expressions.
config.allow_url("*.lvh.me")
end
RSpec:
describe 'invoices' do
let!(:account) { create(:account_with_schema) }
let(:user) { account.owner }
before do
set_subdomain(account.subdomain)
sign_user_in(user)
@customer = create(:customer)
end
describe 'when invoice exists' do
before(:each) do
@invoice = create(:invoice, user: user, customer: @customer,
deadline: Date.tomorrow, payment_term: '')
visit invoices_path
click_link I18n.t('button.show')
expect(page).to have_text @invoice.date_of_an_invoice
expect(page).to have_text @invoice.customer.name
expect(page).to have_text @invoice.reference_number
expect(page).to have_link I18n.t('button.delete')
expect(page).to have_link I18n.t('button.edit')
end
it 'allows invoice to be deleted', js: true do
click_link I18n.t('button.delete')
wait_for_ajax
expect(page).to have_text
I18n.t('invoices.destroy.confirmation_msg')
within('.modal-footer') do
click_link I18n.t('button.delete')
end
expect(page).to have_text I18n.t('invoices.destroy.success_delete')
expect(page).to_not have_text @invoice.date_of_an_invoice
expect(page).to_not have_text @invoice.customer
end
end
end
因此,测试在'当发票存在'描述的before
块中的click_link I18n.t('button.show')行失败。基本上在@invoice实例上找到了“显示”按钮,我执行了save_and_open_page来调查问题并且@invoice实例不存在。
错误:
Failures:
1) invoices when invoice exists allows invoice to be deleted
Failure/Error: click_link I18n.t('button.show')
Capybara::ElementNotFound:
Unable to find link "Show"
# /home/birhanu/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
# /home/birhanu/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
# /home/birhanu/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
# /home/birhanu/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/actions.rb:27:in `click_link'
# /home/birhanu/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
# /home/birhanu/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
# ./spec/features/invoices_feature_spec.rb:77:in `block (3 levels) in <top (required)>'
Finished in 18.5 seconds (files took 6.39 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/features/invoices_feature_spec.rb:110 # invoices when invoice exists allows invoice to be deleted
NB 当不使用js:true标志时,所有测试都按预期传递