这是我的spec/system/group_index_spec.rb
文件。
require_relative '../rails_helper'
RSpec.describe 'Group Index', type: :system do
let(:user) { User.create(name: 'First User', email: 'test@user.com', password: 'password') }
before do
# driven_by(:rack_test)
puts 'Creating test data...'
5.times do |i|
group = Group.create(name: "Group #{i}", author: user)
group.icon.attach(io: File.open(Rails.root.join('spec', 'models', 'files', 'test.jpg')), filename: 'test.jpg',
content_type: 'image/jpeg')
5.times do |j|
Entity.create(name: "Entity #{j}", amount: 100, author: user)
end
end
Group.all.each do |group|
group.entities << Entity.all.sample(rand(1..3))
end
Entity.all.each do |entity|
entity.groups << Group.all.sample(rand(1..3))
end
puts 'Logging in...'
visit new_user_session_path
fill_in 'user_email', with: 'test@user.com'
fill_in 'user_password', with: 'password'
click_button 'Log in'
end
it 'displays all groups', js: true do
visit groups_path
expect(page).to have_content('Group 0')
expect(page).to have_content('Group 1')
expect(page).to have_content('Group 2')
expect(page).to have_content('Group 3')
expect(page).to have_content('Group 4')
end
end
字符串
当我运行这个测试时,我得到这个错误消息。
Creating test data...
Logging in...
F
Failures:
1) Group Index displays all groups
Failure/Error: expect(page).to have_content('Group 0')
expected to find text "Group 0" in "CATEGORIES\nNew"
[Screenshot Image]: /home/shasherazi/programming/microverse/budget-app/tmp/capybara/failures_r_spec_example_groups_g
roup_index_displays_all_groups_713.png
# ./spec/system/group_index_spec.rb:36:in `block (2 levels) in <top (required)>'
Finished in 8.22 seconds (files took 1.64 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/system/group_index_spec.rb:34 # Group Index displays all groups
型
这是一个short video显示的过程。
这是我的rails_helper.rb
文件,我修改了从互联网上的建议。
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'capybara/rspec'
require 'database_cleaner'
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
abort e.to_s.strip
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://rspec.info/features/6-0/rspec-rails
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
Capybara.run_server = true
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"
end
型
在我的开发环境中,一切都很好,我使用faker
以类似的方式创建示例数据。我今天几乎花了一整天的时间来寻找这个问题的解决方案,但没有找到。如果你能帮助我,我将非常感激你。
解决方案
我需要手动调用.save
来实际保存数据。这是正确的测试文件。
require_relative '../rails_helper'
RSpec.describe 'Groups', type: :system do
let(:user) { User.create(name: 'First User', email: 'test@user.com', password: 'password') }
before do
# comment the line below to run tests in browser
driven_by(:rack_test)
puts "\nCreating test data..."
5.times do |i|
group = Group.create(name: "Group #{i}", author: user)
group.icon.attach(io: File.open(Rails.root.join('spec', 'models', 'files', 'test.jpg')), filename: 'test.jpg',
content_type: 'image/jpeg')
group.save
5.times do |j|
entity = Entity.create(name: "Entity #{j}", amount: 100, author: user)
entity.save
end
end
Group.all.each do |group|
group.entities << Entity.all.sample(rand(1..3))
end
Entity.all.each do |entity|
entity.groups << Group.all.sample(rand(1..3))
end
puts 'Logging in...'
visit new_user_session_path
fill_in 'user_email', with: 'test@user.com'
fill_in 'user_password', with: 'password'
click_button 'Log in'
end
after(:all) do
puts "\nCleaning up..."
Group.destroy_all
Entity.destroy_all
User.destroy_all
end
it 'displays all groups, their names, and icons', js: true do
visit groups_path
user.groups.each do |group|
expect(page).to have_content(group.name)
expect(page).to have_css("img[src*='#{group.icon.blob.filename}']")
end
end
end
型
2条答案
按热度按时间cngwdvgl1#
你在这里有这么多的关注数据库方面的事情,但这似乎不是你的问题。
我认为你应该在你的预期之前在测试中使用
pp Group.all
,只是为了确认(为你自己)这不是一个数据库问题。您收到的错误消息似乎表明您实际上是在类别索引页面上(或者组索引页面由于某种原因正在使用类别索引视图):
应在“CATEGORIES\n新建”中找到文本“Group 0”
也许保存在
/home/shasherazi/programming/microverse/budget-app/tmp/capybara/failures_r_spec_example_groups_g roup_index_displays_all_groups_713.png
中的屏幕截图可以帮助您确认这一点,您可以开始故障排除。qij5mzcb2#
这里的问题可能是您的用户实际上没有登录。这是因为操作是异步发生的,您没有执行任何操作来等待登录成功。您的测试正在有效运行
字符串
这将导致
visit groups_path
取消由click_button
调用触发的任何请求,并以未经身份验证的用户身份访问组页面。您可以通过在click_button
之后添加sleep 5
来测试它是否修复了您的问题。如果是这样的话,您需要将sleep
替换为实际查找页面中指示登录已完成的更改的代码,代码如下型