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

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

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

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

Ruby on Rails

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

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

Q&A

解決済

2回答

5320閲覧

バリデーションで設定しているエラーメッセージを表示したい

0522ash

総合スコア0

RSpec

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

Ruby on Rails

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

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

0グッド

0クリップ

投稿2021/03/07 05:14

編集2021/03/07 06:35

前提・実現したいこと

フリマアプリの単体テストで'販売価格は半角数字のみ保存可能であること'という問いに対してバリデーションで設定している2つめのメッセージ「message: "Price Half-width number" 」を表示したい

発生している問題・エラーメッセージ

1) Item 出品機能 出品ができない時 販売価格は半角数字のみ保存可能であること Failure/Error: expect(@item.errors.full_messages).to receive("Price Half-width number") [] does not implement: Price Half-width number # ./spec/models/item_spec.rb:86:in `block (4 levels) in <top (required)>

該当のソースコード

Rails

1controller/items_controller 2 3 4class ItemsController < ApplicationController 5 6 before_action :authenticate_user!, except: [:index, :show] 7 8 9 def index 10 @items = Item.order("created_at DESC") 11 end 12 13 def new 14 @item = Item.new 15 end 16 17 def create 18 @item = Item.new(item_params) 19 if @item.save 20 redirect_to root_path 21 else 22 render :new 23 end 24 end 25 26 private 27 28 def item_params 29 params.require(:item).permit(:title, :detail, :category_id, :condition_id, :shipping_charge_id, :ship_from_id, :guideline_id, :price, :image).merge(user_id: current_user.id) 30 end 31 32end

Rails

1models/item.rb 2 3 4class Item < ApplicationRecord 5 6 belongs_to :user 7 has_one_attached :image 8 9 extend ActiveHash::Associations::ActiveRecordExtensions 10 belongs_to :category 11 belongs_to :condition 12 belongs_to :shipping_charge 13 belongs_to :ship_from 14 belongs_to :guideline 15 16 with_options presence: true do 17 validates :title 18 validates :detail 19 validates :image 20 end 21 22 validates :price, presence: true, format: { with: /\A[0-9]+\z/, message: "Half-width number" }, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9999999, message: "Out of setting range" } 23 24 25 with_options presence: true, numericality: { other_than: 1, message: 'Select'} do 26 validates :category_id 27 validates :condition_id 28 validates :shipping_charge_id 29 validates :ship_from_id 30 validates :guideline_id 31 end 32end

Rails

1factories/item.rb 2 3 4FactoryBot.define do 5 factory :item do 6 title {Faker::Lorem.sentence} 7 image {Faker::Lorem.sentence} 8 detail {Faker::Lorem.sentence} 9 association :user 10 category_id {2} 11 condition_id {2} 12 shipping_charge_id {2} 13 ship_from_id {2} 14 guideline_id {2} 15 price {5000} 16 end 17end 18

Rails

1 2 3models/item_spec.rb 4 5require 'rails_helper' 6RSpec.describe Item, type: :model do 7 before do 8 @item = FactoryBot.build(:item) 9 @item.image = fixture_file_upload('app/assets/images/star.png') 10 11 end 12 13 describe '出品機能' do 14 context '出品ができる時' do 15 it "必須項目が全てあれば登録できること" do 16 expect(@item).to be_valid 17 end 18 it 'ログイン状態のユーザーのみ、商品出品ページへ遷移できること' do 19 @item = FactoryBot.create(:user) 20 expect(@item).to be_valid 21 end 22 end 23 24 25 26 context '出品ができない時' do 27 it '商品画像を1枚つけることが必須であること' do 28 @item.image.key = '' 29 @item.valid? 30 expect(@item.errors.full_messages).to include{"Image can't be blank"} 31 end 32 33 it '商品名が必須であること' do 34 @item.title = '' 35 @item.valid? 36 expect(@item.errors.full_messages).to include("Title can't be blank") 37 end 38 39 it '商品の説明が必須であること' do 40 @item.detail = '' 41 @item.valid? 42 expect(@item.errors.full_messages).to include("Detail can't be blank") 43 end 44 45 it 'カテゴリーの情報が必須であること' do 46 @item.category_id = '' 47 @item.valid? 48 expect(@item.errors.full_messages).to include("Category Select") 49 end 50 51 it '商品の状態についての情報が必須であること' do 52 @item.condition_id = '' 53 @item.valid? 54 expect(@item.errors.full_messages).to include("Condition Select") 55 end 56 57 it '配送料の負担についての情報が必須であること' do 58 @item.shipping_charge_id = '' 59 @item.valid? 60 expect(@item.errors.full_messages).to include("Shipping charge Select") 61 end 62 63 it '発送元の地域についての情報が必須であること' do 64 @item.ship_from_id = '' 65 @item.valid? 66 expect(@item.errors.full_messages).to include("Ship from Select") 67 end 68 69 it '発送までの日数についての情報が必須であること' do 70 @item.guideline_id = '' 71 @item.valid? 72 expect(@item.errors.full_messages).to include("Guideline Select") 73 end 74 75 it '販売価格についての情報が必須であること' do 76 @item.price = '' 77 @item.valid? 78 expect(@item.errors.full_messages).to include("Price can't be blank", "Price Half-width number", "Price Out of setting range") 79 end 80 81 it '売価格は、¥300~¥9,999,999の間のみ保存可能であること' do 82 @item.price = '100' 83 @item.valid? 84 expect(@item.errors.full_messages).to include("Price Out of setting range") 85 end 86 87 it '販売価格は半角数字のみ保存可能であること' do 88 @price = '10000' 89 @item.valid? 90 expect(@item.errors.full_messages).to receive("Price Half-width number") 91 end 92 end 93 end 94end 95

試したこと

今はpriceのバリデーションを一つにまとめていますが分けたとしても同じ状況でした。
どう試しても"Out of setting range"こちらのエラーメッセージしか出ません。
何が原因かもわからずバリデーションをいじっても治りませんでした。
検索もしたのですが当該のような状況にあたる答えも得られませんでした。

よろしくお願いいたします。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答2

0

自己解決

models/item.rbのバリデーションが

Rails

1 validates :price, numericality: { with: /\A[0-9]+\z/, message: 'Half-width number' } 2 validates :price, 3 numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9_999_999, message: 'Out of setting range' }

numericalityが正解でした。

カラムの設定はintegerで数値のみ受け取れている設定であるのであくまでwith: /\A[0-9]+\z/の正規表現は数値のみという可視化をさせているということで納得いきました。

投稿2021/03/07 10:46

0522ash

総合スコア0

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

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

0

他は include で最後だけ receive になってますが、
これを include にしたらこのテストは通るんじゃないでしょうか。

投稿2021/03/07 07:34

neko_daisuki

総合スコア2090

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問