質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

アップロード

アップロードは特定のファイルをウェブサーバに送るプロセスのことを指します。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Active Record

Active Recordは、一つのオブジェクトに対しドメインのロジックとストレージの抽象性を結合するデザインパターンです。

Q&A

解決済

1回答

1995閲覧

画像更新のテストが通らない

ro-ze1106

総合スコア4

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

アップロード

アップロードは特定のファイルをウェブサーバに送るプロセスのことを指します。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Active Record

Active Recordは、一つのオブジェクトに対しドメインのロジックとストレージの抽象性を結合するデザインパターンです。

1グッド

1クリップ

投稿2021/11/21 03:03

railsチュートリアルの6版を参考に画像アップロード機能はActive Storageを使用しています。

その中、rspecでデータの更新処理のテストを書いて見た所、画像の所だけnilになってしまいエラーが出ます。
検索しながらテストを書いてみましたが、画像処理のテストを書いたことがない為、なぜnilになるのか分かりません。
テストを通すにはどうしたらいいかご教授願います。

こちらがエラーと関係のあるファイルです。
エラー
Failure/Error: expect(problem.reload.picture).to include "test_problem2.png"

expected #<ActiveStorage::Attached::One:0x00007f981c49cee0 @name="picture", @record=#<Problem id: 1, study_typ...updated_at: "2021-11-20 19:07:41.323514000 +0900", title: "更新:計算問題", explanation_text: "更新:計算しなさい">> to include "test_problem2.png", but it does not respond to `include?` Diff: @@ -1,17 +1,33 @@ -["test_problem2.png"] +#<ActiveStorage::Attached::One:0x00007f981c49cee0 + @name="picture", + @record= + #<Problem:0x00007f981c4bcf88 + id: 1, + study_type: "更新:算数", + picture: nil, + problem_text: "更新:12×7=", + answer: "更新:84", + problem_explanation: "更新:人が1グループ12人いました。7グループだと何人ですか?", + target_age: "12", + reference: "hensyu-https://www.dainippon-tosho.co.jp/mext/e07.html", + user_id: 1, + created_at: Sat, 20 Nov 2021 19:07:41.086215000 JST +09:00, + updated_at: Sat, 20 Nov 2021 19:07:41.323514000 JST +09:00, + title: "更新:計算問題", + explanation_text: "更新:計算しなさい">>

テスト
spec/system/problems_spec.rb

let!(:user) { create(:user) }
let!(:problem) { create(:problem, :picture, user: user)}

context '問題の更新処理' do
it '有効な更新' do
fill_in '教科', with: '更新:算数'
fill_in 'タイトル名', with: '更新:計算問題'
fill_in '説明文', with: '更新:計算しなさい'
fill_in '問題文', with: '更新:12×7='
fill_in '答え', with: '更新:84'
fill_in '問題解説', with: '更新:人が1グループ12人いました。7グループだと何人ですか?'
fill_in '対象年齢', with: '12'
fill_in '参考用URL', with: 'hensyu-https://www.dainippon-tosho.co.jp/mext/e07.html'
attach_file "problem[picture]", "#{Rails.root}/spec/fixtures/test_problem2.png"
click_button '更新する'
expect(page).to have_content '問題情報が更新されました!'
expect(problem.reload.study_type).to eq '更新:算数'
expect(problem.reload.title).to eq '更新:計算問題'
expect(problem.reload.explanation_text).to eq '更新:計算しなさい'
expect(problem.reload.problem_text).to eq '更新:12×7='
expect(problem.reload.answer).to eq '更新:84'
expect(problem.reload.problem_explanation).to eq '更新:人が1グループ12人いました。7グループだと何人ですか?'
expect(problem.reload.target_age).to eq '12'
expect(problem.reload.reference).to eq 'hensyu-https://www.dainippon-tosho.co.jp/mext/e07.html'
expect(problem.reload.picture).to include "test_problem2.png"
end

画像
spec/fixtures/test_problem2.png

ダミー
spec/factories/problems.rb
FactoryBot.define do
factory :problem do
study_type { '算数' }
title { '計算問題' }
explanation_text { '□を計算して答えなさい。' }
problem_text { '12×7=□' }
answer { '94' }
problem_explanation { '人が1グループ12人いました。7グループだと何人ですか?' }
target_age { 12 }
reference { 'https://www.dainippon-tosho.co.jp/mext/e07.html' }
association :user
created_at { Time.current }
end

trait :picture do
picture { Rack::Test::UploadedFile.new('spec/fixtures/test_problem.png') }
end
end

ビュー
app/views/problems/edit.html.erb
<span class="picture">
<%= f.label :picture %> <span class="input-unneed">※任意</span>
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png', class: 'form-control', id: 'problem_picture' %>
</span>

study_matome👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ruby

1expect(problem.reload.picture).to include "test_problem2.png"

ログを見ればわかりますが、problem.picture は ActiveStorage::Attached::One のオブジェクトです。
これは but it does not respond to include? とあるように include? に応答しません。
また、ファイル名ではないのでテストは通りません。

problem.picture.filename.to_s でファイル名を得ることができます。

ruby

1# include でもいいですが、eq の方が厳密なので eq にしています 2expect(problem.reload.picture.filename.to_s).to eq "test_problem2.png"

投稿2021/11/21 09:54

neko_daisuki

総合スコア2090

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ro-ze1106

2021/11/22 11:13

ご回答ありがとうございます。 無事テストが通りました。 つまりproblem.pictureのファイル名が文字列オブジェクトに変換できていなかったので、テストに通らなかったと言うことですか?
neko_daisuki

2021/11/22 11:26

はい。problem.picture に関連するファイル名が取得できていませんでした。 problem.picture はファイル名そのものではないので、ファイル名をテストしたいのであれば それを取り出してテストする必要があります。
ro-ze1106

2021/11/22 12:37

詳しく説明して頂きありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問