【问题标题】:RSpec ActiveStorage: Error ActiveRecord::RecordNotFound: Couldn't find ActiveStorage::Blob with 'id'RSpec ActiveStorage:错误 ActiveRecord::RecordNotFound:找不到带有“id”的 ActiveStorage::Blob
【发布时间】:2021-03-14 01:54:48
【问题描述】:

大家好,我可以请大家帮忙看看我的 RSpec 测试出了什么问题吗?我一直在寻找整个互联网,但没有找到任何可以指出我收到此错误的原因。

我想要做的是测试一个实例变量是否有一些来自数据库的数据。当测试到达创建活动记录的 Fabricator 以及 ActiveStorage Blob 时,就会发生错误。我已经添加了数据库清理器 gem,但不确定有什么东西搞砸了,或者我在使用 RSpec、活动存储和 DBcleaner 时遗漏了一些东西。

奇怪的是,我有另一个测试也创建了相同的制造对象,但我没有收到您将在下面看到的错误。如果我注释掉下面的测试,其他测试运行得很好。任何帮助将非常感激。被困在这几个小时:$

更新:我试图查看图形制作器运行后以及使用附件查看附件时发生了什么?方法每个文件实际上是附加的。所有四个文件都以附件的形式返回。我认为清洁器会出现问题,所以我添加到 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

【问题讨论】:

    标签: ruby-on-rails rspec rails-activestorage


    【解决方案1】:

    您的工厂的问题是属性值只在文件加载时评估一次。然后它们被重用于所有对象。

    thumbnail_image ActiveStorage::Blob.create_after_upload!(...).signed_id
    

    在此处使用块

    thumbnail_image { ActiveStorage::Blob.create_after_upload!(...).signed_id }
    

    这样,对于每个新制造的对象,您都会获得自己的 blob。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多