Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly do model testing with ActiveStorage in rails?

I have just switched to using ActiveStorage on rails 5.1.4 and I am new to TDD and struggling to figure out how to test a model that 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 

But I am getting the following error:

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

Any hints?

like image 210
Donato Azevedo Avatar asked Nov 30 '17 22:11

Donato Azevedo


People also ask

How do I test a Rails controller?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.

How active storage works in Rails?

Using Active Storage, an application can transform image uploads or generate image representations of non-image uploads like PDFs and videos, and extract metadata from arbitrary files.

What type of test is not typical in a Rails application?

Don't write view tests. You should be able to change copy or HTML classes without breaking your tests. Just assess critical view elements as part of your in-browser integration tests.


1 Answers

I solved using

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 

After

describe '#image' do   subject { create(:culture).image }    it { is_expected.to be_an_instance_of(ActiveStorage::Attached::One) } end 
like image 160
vgsantoniazzi Avatar answered Sep 19 '22 07:09

vgsantoniazzi