Rspec is unable to configure the audio file and ActiveStorage::IntegrityError: cannot be resolved.

Asked 2 years ago, Updated 2 years ago, 93 views

What do you want to solve

I am a programming beginner currently working on my portfolio.I thought I'd use Rspec to write tests, so I'm using Rspec to create tests.However, an error occurred while writing the audio file verification test.

Error

Failures:

  1) PictumUser test for PictumUser when user input invalid parameter when user is deleted gogaku is deleted too
     Failure/Error: expect(Gogaku.count).to eq1

       expected:1
            got:0

       (compared using==)
     # ./spec/models/dictum_user_spec.rb: 126: in `block(5 levels) in <top(required)>'

  2) Gogaku test for gogakugaku should be valid
     Failure/Error: expect(gogaku).to be_valid
       expected#<Gogakuid:nil,subject:"English",body:"intrip",file:nil,answer:"none",dictum_user_id:nil,created_at:nil,updated_at:nil>to be valid,but got errors:Dictum user can be blank
     # ./spec/models/gogaku_spec.rb: 17: in `block(3 levels) in<main>'

Finished in 0.31763 seconds (files book 1.52 seconds to load)
11 examples, 2 failures

Failed examples:

rspec./spec/models/dictum_user_spec.rb:119#DictumUser test for PictumUser when user input invalid parameter when user is deleted too
rspec./spec/models/gogaku_spec.rb:13 #Gogaku test for gogakugaku should be valid

Affected Source Codes

#/spec/factories/dictum_users.rb

FactoryBot.definedo
  factory —dictum_user do
    sequence(:name) {|n | "user#{n}"}
    sequence(:email) {|n|"test#{n}@e-xample.com"}
    password {"password"}
    password_confirmation {"password"}
  end

#/spec/factories/gogakus.rb

 FactoryBot.definedo
  factory —Gogakudo
      subject {"English"}
      body {"intrip"}
      after(:build) do|gogaku|
          gogaku.file=fixture_file_upload(Rails.root.join('spec', 'fixes', 'files', 'Night_Sea.mp3'), 'audio/mp3', :binary)
      end
      answer {"none"}
      association —dictum_user
  end
end

# spec/models/dictum_user_spec.rb

require 'rails_helper'

RSpec.describeDictumUser, type: :model do
    describe "test for PictumUser" do
        context "should be valid" do
            it "create instance accumulate" do
                expect(user).to be_valid
            end
        end
        context "when user input invalid parameter" do
            context "when name which user input is empty" do
                user=FactoryBot.build(:dictum_user,name:"")
                it "regard the user as invalid" do
                    expect(user).not_to be_valid
                end
            end
            context "when name which user input is too long" do
                user=FactoryBot.build(:dictum_user,name:"a"*51)
                it "regard the user as invalid" do
                    expect(user).not_to be_valid
                end
            end
            
            context "when email which user input is empty" do
                user=FactoryBot.build(:dictum_user, email:"")
                it "regard the user as invalid" do
                    expect(user).not_to be_valid
                end
            end
            context "when email which user input is invalid format" do
                user=FactoryBot.build(:dictum_user, email: "email.com")
                it "regard the user as invalid" do
                    expect(user).not_to be_valid
                end
            end
            context "when email which user input is too long" do
                user=FactoryBot.build(:dictum_user, email: "a"*244+"@e-xample.com")
                it "regard the user as invalid" do
                    expect(user).not_to be_valid
                end
            end
            context "when password which user input is empty" do
                user=FactoryBot.build(:dictum_user, password:"*6, password_confirmation:"*6)
                it "regard the user as invalid" do
                    expect(user).not_to be_valid
                end
            end
            context "when password which user input is too short" do
                user=FactoryBot.build(:dictum_user, password: "hoge", password_confirmation: "hoge")
                it "regard the user as invalid" do
                    expect(user).not_to be_valid
                end
            end
            context "when email which user input is not `tunique" do
                user=FactoryBot.build(:dictum_user)
                it "regard the user as invalid" do
                    user.save
                    user2 = user.dup
                    user2.email=user2.email.upcase
                    user2.save
                    expect(user2).not_to be_valid
                end
            end
            context "when user is deleted" do
                user=FactoryBot.build(:dictum_user)
                gogaku=FactoryBot.build(:gogaku,dictum_user:user)
                
                It "gogaku is deleted too" do
                    expect(Gogaku.count).to eq1
                    user.destroy
                    expect(Gogaku.count).to eq0
                end
            end
        end
    end     
end

#/spec/models/gogaku_spec.rb

require 'rails_helper'

RSpec.describe Gogaku, type: :model do
  describe "test for gogaku" do
    let(:dictum_user) {FactoryBot.build(:dictum_user)}
    let(:gogaku) {FactoryBot.build(:gogaku,dictum_user:dictum_user)}
    It "gogaku should be valid" do
      expect(gogaku).to be_valid
    end
  end
end


 #gogaku.rb

class Gogaku<ApplicationRecord
    belongs_to —dictum_user
    default_scope->{order(created_at::desc)}
    values:subject, presence:true
    values:body, presence:true
    has_one_attached:file
    values:dictum_user_id, presence:true
    values:file,presence:true
    validates:file,size:{less_than:5.megabytes,
                                      message: "should be less than 5MB"}
end

# 


What I tried myself

https://gist.github.com/h-sakano/50092b1c17fe8ddc0bbe5382fb8587a4
https://qiita.com/h-sakano/items/fc297f91a7bafc0b4d6d
·First of all, I referred to their articles, but in my case,

#spec/factories/dictum_users.rb
after(:build) do|gogaku|
        gogaku.file=fixture_file_upload(Rails.root.join('spec', 'fixes', 'files', 'Night_Sea.mp3'), 'audio/mpeg', :binary)
    end

This description did not correct the error.
·Next
https://qiita.com/tatematsu-k/items/1cdd946cd38e69d16340
I used this article as a reference.As I was using rails 6.1.5 I will inquire about the following

#rails_helper.rb

FactoryBot::SyntaxRunner.class_evaldo
    include ActionDispatch::TestProcess
    include ActiveSupport::Testing::FileFixtures
  end

However, the error has not been corrected.

"·For some reason, the ""when user is deleted gogaku is deleted too"" part has an error that says ""Dictum_user is empty."""This may be due to a has_many relationship in the model (DictumUser is the parent Gogaku is the child).
https://qiita.com/takehanKosuke/items/ae324483e7f9451bf6a7
https://qiita.com/__kotaro_/items/adbc355bfb550b8b2150
https://qiita.com/johnslith/items/c0b2a9b8ce8770e5d317
I used their articles as reference to check if the association is correct.I think it's working, but somehow it doesn't work.

Other

·Ruby 3.0.3 rails 6.1.5 environment was built by Docker.I am using Ubuntu from wsl2.
·Also, I am doing multi-posting, although it is not recommended.
https://teratail.com/questions/rs7c1n0nhk64nu

Add

Add

https://insyokuprogram.com/2021/08/08/%E3%80%90ruby-on-rails%E3%80%91to-be-valid-but-got-errors-user-cant-be-blank-%E3%80%90rspec%E3%80%91%E3%81%8C%E8%A7%A3%E6%B1%BA%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%8B%E3%81%A3%E3%81%9F%E4%BB%B6/
Based on this article, when I changed the part that says build to create, the error changed.

An error occurred while loading./spec/models/dictum_user_spec.rb.
Failure/Error:gogaku=FactoryBot.create(:gogaku,dictum_user:user)

ActiveStorage:: IntegrityError:
  ActiveStorage:: IntegrityError
# /usr/local/bundle/gems/activestorage-6.1.5/lib/active_storage/service/disk_service.rb: 159:in`ensure_integrity_of'
# /usr/local/bundle/gems/activestorage-6.1.5/lib/active_storage/service/disk_service.rb:22:in `block in upload'
# /usr/local/bundle/gems/activestorage-6.1.5/lib/active_storage/service.rb: 155:in`instrument'
# /usr/local/bundle/gems/activestorage-6.1.5/lib/active_storage/service/disk_service.rb:20:in `upload'
# /usr/local/bundle/gems/activestorage-6.1.5/app/models/active_storage/blob.rb: 253:in `upload_without_unfurling'
# /usr/local/bundle/gems/activestorage-6.1.5/lib/active_storage/attached/changes/create_one.rb:26:in `upload'
# /usr/local/bundle/gems/activestorage-6.1.5/lib/active_storage/attached/model.rb:77:in `block in has_one_attached'
# /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/validation.rb:18:in `create'
# /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/strategy/create.rb:12:in `block in result'
# <internal:kernel>:90:in`tap'
# /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/strategy/create.rb:9:in `result'
# /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/factory.rb:43:in `run'
# /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/factory_runner.rb:29:in `block in run'
# /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/factory_runner.rb:28:in `run'
# /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/strategy_syntax_method_registrar.rb: 28: in `block in define_singular_strategy_method'
# ./spec/models/dictum_user_spec.rb: 117: in `block(4 levels) in <top(required)>'
# ./spec/models/dictum_user_spec.rb: 114: in `block(3 levels) in <top(required)>'
# ./spec/models/dictum_user_spec.rb: 27: in `block(2 levels) in <top(required)>'
# ./spec/models/dictum_user_spec.rb:5:in`block in<top(required)>'
# ./spec/models/dictum_user_spec.rb:3:in`<top(required)>'

Finished in 0.00009 seconds (files book 1.79 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

Debugging is currently underway here.

I'm a beginner in programming, so I might have misunderstood something wrong.If you have any advice, please let me know.

ruby-on-rails ruby rspec wsl

2022-09-30 11:31

1 Answers

It was solved without any problems.
https://www.reddit.com/r/rails/comments/na2gu1/anyone_facing_activestorageintegrityerror_recently/
Based on this article, I wrote the code as below, and it worked well.

#src/spec/factories/gogakus.rb
FactoryBot.definedo
  factory —Gogakudo
      subject {"English"}
      body {"intrip"}
      after(:build) do|gogaku|
        image_file=File.open("./spec/fixes/files/Night_Sea.mp3")
        gogaku.file.attach(io:image_file, filename: "Night_Sea.mp3", content_type: 'audio/mp3')
        image_file.rewind
      end
      answer {"none"}
      association —dictum_user
  end
end


2022-09-30 11:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.