RSpec ActiveStorage:错误 ActiveRecord::RecordNotFound:找不到 ActiveStorage::Blob 和 'id'
RSpec ActiveStorage: Error ActiveRecord::RecordNotFound: Couldn't find ActiveStorage::Blob with 'id'
大家好,我能得到一些帮助来弄清楚我的 RSpec 测试出了什么问题吗?我在互联网上到处寻找,但没有找到任何可以说明我收到此错误的原因。
我想做的是测试一个实例变量是否有一些来自数据库的数据。当测试到达创建活动记录和 ActiveStorage Blob 的制造商时,就会发生错误。我已经添加了数据库清理器 gem 但不确定是不是有什么问题,或者我在使用 RSpec、活动存储和 DBcleaner 时遗漏了什么。
st运行ge 是什么,我有另一个测试也创建了相同的制造对象,但我没有收到您将在下面看到的错误。如果我注释掉下面的测试,其他测试运行得很好。任何帮助将非常感激。坚持了几个小时:$
更新:我试图调查图形制作者之后发生的事情 运行 以及当我使用附件查看附件时?方法每个文件实际上是附加的。所有四个文件都按附件返回。我以为清洁器会发生一些事情,所以我添加到 rails_helper 以在每次测试完成后清除所有文件。
config.after(:each) do
DatabaseCleaner.clean
ActiveStorage::Attachment.all.each { |attachment| attachment.purge }
end
这应该会抓取所有附件并在每次测试运行后删除附件和 blob。但这没有做任何事情。仍然不确定发生了什么。
如有任何帮助,我们将不胜感激。
谢谢:)
错误:
1) Dashboard::VendorDashboardsController GET sales Authenticated assigns instance variable with purchases for that store
Failure/Error: graphic = Fabricate(:graphic, store:store)
ActiveRecord::RecordNotFound:
Couldn't find ActiveStorage::Blob with 'id'=1
# ./spec/controllers/Dashboard/vendor_dashboards_controller_spec.rb:26:in `block (4 levels) in <top (required)>'
RSPEC 测试
it "assigns instance variable with purchases for that store" do
@user = Fabricate(:user)
vendor = Fabricate(:vendor, user:@user)
store = Fabricate(:store, vendor:vendor)
graphic = Fabricate(:graphic, store:store)
purchase_one = Fabricate(:purchase, purchasable_id:graphic.id)
purchase_two = Fabricate(:purchase, purchasable_id:graphic.id)
@user.add_role(:vendor)
login_user(@user)
get :sales
expect(assigns(:sales)).to eq(Purchase.where(store_id:store.id))
end
制造商
Fabricator(:graphic)do
store
name "Icon Set"
language "Spanish"
standard_license_price 10.00
business_license_price 100.00
reviewed true
status "Approved"
files_included "PSD, CSS"
category "Vectors"
subject "Characters"
support "3 Month Support"
layered true
high_resolution true
pixel_dimensions "2000x2000"
software_required "Illustrator"
thumbnail_image ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'Advertise.png'), 'rb'), filename: 'Advertise.png',content_type: 'image/png').signed_id
main_image ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'Chat.png'), 'rb'), filename: 'Chat.png',content_type: 'image/png').signed_id
product_images ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'Chat.png'), 'rb'), filename: 'Chat.png',content_type: 'image/png').signed_id
graphic_asset_files ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'test_illustrations.zip'), 'rb'), filename: 'test_illustrations.zip',content_type: 'application/zip').signed_id
description "This is the best graphic design"
end
RAILS 帮助文件
# 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', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
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 = false
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
config.include Rails.application.routes.url_helpers
config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
config.include Sorcery::TestHelpers::Rails::Integration, type: :feature
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
end
您工厂的问题是 属性 值仅在文件加载时评估一次。然后它们被重新用于所有对象。
thumbnail_image ActiveStorage::Blob.create_after_upload!(...).signed_id
在这里使用一个块
thumbnail_image { ActiveStorage::Blob.create_after_upload!(...).signed_id }
这样,对于每个新制作的对象,您都会得到它自己的 blob。
大家好,我能得到一些帮助来弄清楚我的 RSpec 测试出了什么问题吗?我在互联网上到处寻找,但没有找到任何可以说明我收到此错误的原因。
我想做的是测试一个实例变量是否有一些来自数据库的数据。当测试到达创建活动记录和 ActiveStorage Blob 的制造商时,就会发生错误。我已经添加了数据库清理器 gem 但不确定是不是有什么问题,或者我在使用 RSpec、活动存储和 DBcleaner 时遗漏了什么。
st运行ge 是什么,我有另一个测试也创建了相同的制造对象,但我没有收到您将在下面看到的错误。如果我注释掉下面的测试,其他测试运行得很好。任何帮助将非常感激。坚持了几个小时:$
更新:我试图调查图形制作者之后发生的事情 运行 以及当我使用附件查看附件时?方法每个文件实际上是附加的。所有四个文件都按附件返回。我以为清洁器会发生一些事情,所以我添加到 rails_helper 以在每次测试完成后清除所有文件。
config.after(:each) do
DatabaseCleaner.clean
ActiveStorage::Attachment.all.each { |attachment| attachment.purge }
end
这应该会抓取所有附件并在每次测试运行后删除附件和 blob。但这没有做任何事情。仍然不确定发生了什么。
如有任何帮助,我们将不胜感激。
谢谢:)
错误:
1) Dashboard::VendorDashboardsController GET sales Authenticated assigns instance variable with purchases for that store
Failure/Error: graphic = Fabricate(:graphic, store:store)
ActiveRecord::RecordNotFound:
Couldn't find ActiveStorage::Blob with 'id'=1
# ./spec/controllers/Dashboard/vendor_dashboards_controller_spec.rb:26:in `block (4 levels) in <top (required)>'
RSPEC 测试
it "assigns instance variable with purchases for that store" do
@user = Fabricate(:user)
vendor = Fabricate(:vendor, user:@user)
store = Fabricate(:store, vendor:vendor)
graphic = Fabricate(:graphic, store:store)
purchase_one = Fabricate(:purchase, purchasable_id:graphic.id)
purchase_two = Fabricate(:purchase, purchasable_id:graphic.id)
@user.add_role(:vendor)
login_user(@user)
get :sales
expect(assigns(:sales)).to eq(Purchase.where(store_id:store.id))
end
制造商
Fabricator(:graphic)do
store
name "Icon Set"
language "Spanish"
standard_license_price 10.00
business_license_price 100.00
reviewed true
status "Approved"
files_included "PSD, CSS"
category "Vectors"
subject "Characters"
support "3 Month Support"
layered true
high_resolution true
pixel_dimensions "2000x2000"
software_required "Illustrator"
thumbnail_image ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'Advertise.png'), 'rb'), filename: 'Advertise.png',content_type: 'image/png').signed_id
main_image ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'Chat.png'), 'rb'), filename: 'Chat.png',content_type: 'image/png').signed_id
product_images ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'Chat.png'), 'rb'), filename: 'Chat.png',content_type: 'image/png').signed_id
graphic_asset_files ActiveStorage::Blob.create_after_upload!(io: File.open(Rails.root.join('spec', 'support', 'assets', 'graphics', 'test_illustrations.zip'), 'rb'), filename: 'test_illustrations.zip',content_type: 'application/zip').signed_id
description "This is the best graphic design"
end
RAILS 帮助文件
# 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', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
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 = false
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
config.include Rails.application.routes.url_helpers
config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
config.include Sorcery::TestHelpers::Rails::Integration, type: :feature
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
end
您工厂的问题是 属性 值仅在文件加载时评估一次。然后它们被重新用于所有对象。
thumbnail_image ActiveStorage::Blob.create_after_upload!(...).signed_id
在这里使用一个块
thumbnail_image { ActiveStorage::Blob.create_after_upload!(...).signed_id }
这样,对于每个新制作的对象,您都会得到它自己的 blob。