尽管使用了 JavaScript 警报 window,但销毁方法测试并未失败
Destroy method test doesn't fail, although JavaScript alert window is used
我有一个 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 'Destroy'
# expect { click_link 'Destroy' }.to change(User, :count).by(-1)
expect(page).to have_content 'User was successfully destroyed'
end
end
我在浏览器中有以下 link:
<%= link_to "Destroy", user_path(user), method: :delete, data: { confirm: "Are you sure?" } %>
我运行测试,测试通过。它不应该通过,因为在调用操作之前我有警报 window。
我安装了
brew cask install chromedriver
因为我想测试 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_driver
/javascript_driver
),而且您的测试中也没有 js
元数据,这意味着您可能使用 rack_test 驱动程序 运行 你的测试。 rack_test
驱动不支持 JS,因此会完全忽略 JS 确认。如果你想测试 JS 行为,你需要确保你使用的是真正支持 JS 的驱动程序 - 请参阅 https://github.com/teamcapybara/capybara#drivers and https://github.com/teamcapybara/capybara#using-capybara-with-rspec
我有一个 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 'Destroy'
# expect { click_link 'Destroy' }.to change(User, :count).by(-1)
expect(page).to have_content 'User was successfully destroyed'
end
end
我在浏览器中有以下 link:
<%= link_to "Destroy", user_path(user), method: :delete, data: { confirm: "Are you sure?" } %>
我运行测试,测试通过。它不应该通过,因为在调用操作之前我有警报 window。
我安装了
brew cask install chromedriver
因为我想测试 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_driver
/javascript_driver
),而且您的测试中也没有 js
元数据,这意味着您可能使用 rack_test 驱动程序 运行 你的测试。 rack_test
驱动不支持 JS,因此会完全忽略 JS 确认。如果你想测试 JS 行为,你需要确保你使用的是真正支持 JS 的驱动程序 - 请参阅 https://github.com/teamcapybara/capybara#drivers and https://github.com/teamcapybara/capybara#using-capybara-with-rspec