我有一个Rails应用程序,已通过RSpec和Capybara进行了测试
我有以下测试:
<a class="btn btn-primary" href="{{route('payments.viewpayment',$vehicles->id)}}" role="button">Go Premium</a>
<form method="post" action="{{route('payments.payment', ['id' => $vehicles->id])}}">
{{csrf_field()}}
我在浏览器中具有以下链接:
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
brew cask install chromedriver
一旦测试失败,我将通过以下方式改进测试:
# 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
我认为我的问题是,我不知道将线放在何处:
Capybara.default_driver =:selenium_chrome ###不起作用
在哪里为水豚的配置创建文件?在官方文档中,他们在lib / capybara.rb
中说我在那里尝试过,但出现错误:
accept_confirm do
click_link 'Destroy'
end
答案 0 :(得分:1)
您没有显示Capybara配置(default_driver
/ javascript_driver
),并且测试中也没有js
元数据,这暗示您可能正在使用rack_test驱动程序以运行您的测试。 rack_test
驱动程序不支持JS,因此将完全忽略JS确认。如果要测试JS行为,则需要确保使用的是实际支持JS的驱动程序-请参见https://github.com/teamcapybara/capybara#drivers和https://github.com/teamcapybara/capybara#using-capybara-with-rspec