前提・実現したいこと
音声ファイルをアップロードして保存するモデルのテストコードを実装したいのですが、
音声ファイルがうまくテストコードに実装できずエラーが出てしまいます。
これを解決しテストコードを正常に実装したいです。
音声ファイルをアップロードの実装はcarrierwaveで実装しています。
発生している問題・エラーメッセージ
Failure/Error: song.file(io: File.open('public/songs/01 Starry night shadow_1.m4a'), filename: '01 Starry night shadow_1.m4a') ArgumentError: wrong number of arguments (given 1, expected 0) # ./spec/factories/songs.rb:12:in `block (3 levels) in <main>' # ./spec/models/song_spec.rb:4:in `block (2 levels) in <top (required)>'
該当のソースコード
spec/factories/songs.rb
ruby
1FactoryBot.define do 2 factory :song do 3 title { Faker::Name.initials(number: 2) } 4 info { 'test' } 5 writer { 'test' } 6 composer { 'test' } 7 lyric { 'test' } 8 association :artist 9 10 after(:build) do |song| 11 song.image.attach(io: File.open('public/images/bandsample.jpg'), filename: 'bandsample.jpg') 12 song.file(io: File.open('public/songs/01 Starry night shadow_1.m4a'), filename: '01 Starry night shadow_1.m4a') 13 end 14 end 15end
spec/models/song_spec.rb
ruby
1require 'rails_helper' 2RSpec.describe Song, type: :model do 3 before do 4 @song = FactoryBot.build(:song) 5 end 6 7 describe '楽曲アップロード' do 8 context '楽曲アップロードできる場合' do 9 it '必須項目が存在すれば登録できる' do 10 expect(@song).to be_valid 11 end 12 end 13 end 14end
app/models/song.rb
ruby
1class Song < ApplicationRecord 2 is_impressionable counter_cache: true 3 4 mount_uploader :file, AudiofileUploader 5 belongs_to :artist 6 has_one_attached :image 7 8 validates :title, presence: true 9 validates :composer, presence: true 10 validates :image, presence: true 11 validates :file, presence: true 12end
試したこと
webで検索してみたのですが欲しい答えが見つからなかったので試せていないです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。