Ruby on rails 破坏方法试验不';t失败,尽管使用了JavaScript警报窗口
我有一个Rails应用程序,使用RSpec和Capybara进行测试 我有以下测试:Ruby on rails 破坏方法试验不';t失败,尽管使用了JavaScript警报窗口,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,我有一个Rails应用程序,使用RSpec和Capybara进行测试 我有以下测试: context 'destroy user' do scenario "should be successful" do user = User.create(first_name: 'John', last_name: 'Doe', email: 'john.doe@example.com') visit users_path click_link 'Destro
context 'destroy user' do
scenario "should be successful" do
user = User.create(first_name: 'John', last_name: 'Doe', email: 'john.doe@example.com')
visit users_path
click_link 'Destroy'
# expect { click_link 'Destroy' }.to change(User, :count).by(-1)
expect(page).to have_content 'User was successfully destroyed'
end
end
我在浏览器中有以下链接:
<%= link_to "Destroy", user_path(user), method: :delete, data: { confirm: "Are you sure?" } %>
因为我想测试JS,但不幸的是测试并没有失败
rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
# Add additional requires below this line. Rails is not loaded until this point!
require 'rspec/rails'
require 'capybara/rails'
# Capybara.default_driver = :selenium_chrome ### not working
# require "support/factory_bot"
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
end
一旦测试失败,我将通过以下方法改进测试:
accept_confirm do
click_link 'Destroy'
end
我想我的问题是,我不知道该把线放在哪里:
Capybara.default_driver=:selenium_chrome####不工作
我在哪里为水豚的配置创建文件?在官方文件中,他们在lib/capybara.rb中说
我在那里试过了,我得到了一个错误:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
Bundler::GemRequireError:
There was an error while trying to load the gem 'capybara'.
Gem Load Error is: uninitialized constant Capybara
Backtrace for gem load error is:
/Users/albert/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `block in load_missing_constant'
/Users/albert/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
您没有显示您的Capybara配置(
default\u driver
/javascript\u driver
),测试中也没有js
元数据,这意味着您可能正在使用机架测试驱动程序运行测试。rack_test
驱动程序不支持JS,因此将完全忽略JS确认。如果您想测试JS行为,您需要确保您使用的驱动程序实际上支持JS-请参阅和我不确定“官方文档”告诉您如何在lib/Capybara.rb
中配置Capybara,但它应该配置在一个仅在测试运行期间加载的文件中。通常,这与RSpec的配置位置相同(使用RSpec时),因此rails_helper.rb或spec_helpers.rb取决于您的具体设置。您还应该在spec_helper.rb中要求使用capybara/rspec,如capybara文档中所述-
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
Bundler::GemRequireError:
There was an error while trying to load the gem 'capybara'.
Gem Load Error is: uninitialized constant Capybara
Backtrace for gem load error is:
/Users/albert/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `block in load_missing_constant'
/Users/albert/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'