40

我刚刚切换到在 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

但我收到以下错误:

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)>'

有什么提示吗?

4

7 回答 7

30

我解决了使用

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

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

  it { is_expected.to be_an_instance_of(ActiveStorage::Attached::One) }
end
于 2018-04-17T01:07:25.883 回答
18

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

config.active_storage.service = :test

到 config/environments/test.rb 文件

于 2017-11-30T23:32:19.110 回答
8

这是我解决它的方法

# model
class Report < ApplicationRecord
  has_one_attached :file
end
# factory
FactoryBot.define do
  factory :report, class: Report do 
    any_extra_field { 'its value' }
  end
end
# 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

希望对你有帮助

于 2020-05-05T01:22:08.090 回答
3

在 config/environments/test.rb

config.active_storage.service = :test

在您的规格中

it {expect(valid_user.avatar).to be_attached}

于 2019-01-11T22:41:28.447 回答
2

我遇到了类似的 nil 错误(Module::DelegationError: to_model 委托给附件,但附件是 nil),但它只发生在多个测试使用同一个工厂时。

问题似乎是先前测试的拆解中的清理在下一次工厂调用附加后删除了该文件。关键是将 ActiveJob 队列适配器更新为内联,以便立即删除文件。

将这两个设置添加到 config/environments/test.rb:

# Use inline job processing to make things happen immediately
config.active_job.queue_adapter = :inline

# Store uploaded files on the local file system in a temporary directory.
config.active_storage.service = :test
于 2020-06-10T18:48:39.123 回答
1

为什么不:


RSpec.describe User, type: :model do
  describe 'relations' do
    it { is_expected.to have_one(:avatar_attachment) }
  end
end
于 2021-03-25T16:49:19.280 回答
1

在这些帖子的帮助下,我对 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

现在您只能添加更多测试

于 2021-12-23T08:20:46.247 回答