如何在rails中使用ActiveStorage正确进行模型测试?

Don*_*edo 24 rspec ruby-on-rails rails-activestorage

我刚刚转向在rails 5.1.4上使用ActiveStorage而且我是TDD的新手并且正在努力弄清楚如何测试模型 has_one_attached :avatar

require 'rails_helper'

RSpec.describe User, :type => :model do

  let (:valid_user) { FactoryBot.build(:user) }
  describe "Upload avatar" do
    context "with a valid image" do      
      it "saves the image" do
        valid_user.save!        
        saved_file = valid_user.avatar.attach(io: File.open("/home/ubuntu/workspace/spec/fixtures/files/avatar.jpg"), filename: "face.jpg", content_type: "image/jpg")
        expect(saved_file).to be_an_instance_of(ActiveStorage::Attachment::One)
      end
    end
  end 

end
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

Failures:

  1) User Upload avatar with a valid image saves the image
     Failure/Error:
       saved_file = valid_user.avatar.attach(io: File.open("/home/ubuntu/workspace/spec/fixtures/files/avatar.jpg"), filename: "face.jpg", 
                                             content_type: "image/jpg")


 NoMethodError:
   undefined method `upload' for nil:NilClass
   Did you mean?  load
 # /usr/local/rvm/gems/ruby-2.3.4/gems/activestorage-0.1/lib/active_storage/blob.rb:48:in `upload'
 # /usr/local/rvm/gems/ruby-2.3.4/gems/activestorage-0.1/lib/active_storage/blob.rb:21:in `block in build_after_upload'
 # /usr/local/rvm/gems/ruby-2.3.4/gems/activestorage-0.1/lib/active_storage/blob.rb:16:in `tap'
 # /usr/local/rvm/gems/ruby-2.3.4/gems/activestorage-0.1/lib/active_storage/blob.rb:16:in `build_after_upload'
 # /usr/local/rvm/gems/ruby-2.3.4/gems/activestorage-0.1/lib/active_storage/blob.rb:26:in `create_after_upload!'
 # /usr/local/rvm/gems/ruby-2.3.4/gems/activestorage-0.1/lib/active_storage/attached.rb:25:in `create_blob_from'
 # /usr/local/rvm/gems/ruby-2.3.4/gems/activestorage-0.1/lib/active_storage/attached/one.rb:9:in `attach'
 # ./spec/models/user_spec.rb:47:in `block (4 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

任何提示?

小智 16

我解决了使用

FactoryBot.define do
  factory :culture do
    name 'Soy'
    after(:build) do |culture|
      culture.image.attach(io: File.open(Rails.root.join('spec', 'factories', 'images', 'soy.jpeg')), filename: 'soy.jpeg', content_type: 'image/jpeg')
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

describe '#image' do
  subject { create(:culture).image }

  it { is_expected.to be_an_instance_of(ActiveStorage::Attached::One) }
end
Run Code Online (Sandbox Code Playgroud)

  • 这会给你一个可能的误报,因为图像将永远是ActiveStorage :: Attached :: One的一个实例,即使它是nil.在控制台中尝试以下操作:`User.new.avatar`.请注意,它是ActiveStorage :: Attached :: One的一个实例. (2认同)

Don*_*edo 15

问题解决了.在将错误跟踪到ActiveStorage :: Blob :: upload方法后,它说:Uploads the io to the service on the key for this blob.我意识到我没有为Test环境设置active_storage.service.简单地说,只需添加:

config.active_storage.service = :test
Run Code Online (Sandbox Code Playgroud)

配置/ environments/test.rb文件

  • 不高兴地尝试了另一个答案 - 这对我有用,谢谢 (2认同)

小智 7

这是我解决它的方法

# model
class Report < ApplicationRecord
  has_one_attached :file
end
Run Code Online (Sandbox Code Playgroud)
# factory
FactoryBot.define do
  factory :report, class: Report do 
    any_extra_field { 'its value' }
  end
end
Run Code Online (Sandbox Code Playgroud)
# spec
require 'rails_helper'
RSpec.describe Report, type: :model do
  context "with a valid file" do
    before(:each) do
      @report = create(:report)
    end

    it "is attached" do
      @report.file.attach(
        io: File.open(Rails.root.join('spec', 'fixtures', 'file_name.xlsx')),
        filename: 'filename.xlsx',
        content_type: 'application/xlsx'
      )
      expect(@report.file).to be_attached
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我希望它能帮助你


Mik*_*odt 5

在这些帖子的帮助下,我更新了 Rails 6.1 上主动存储的 rspec 测试

# .\spec\models\activestorage_spec.rb
require 'rails_helper'

RSpec.describe User, :type => :model do
  before(:each) do
    @user = FactoryBot.create(:user)
        @user.save!
        saved_file = @user.pictures.attach(io: File.open("./storage/test.jpg"), filename: "test.jpg", content_type: "image/jpg")
  end

  describe "Upload picture" do
    context "with a valid picture" do    

      it "saves the picture" do        
        expect(@user.pictures).to be_attached
      end

    end
  end
end
Run Code Online (Sandbox Code Playgroud)

现在你只能添加一些更多的测试