0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

単体テストコードで解決したこと

Posted at
spec/models/item_spec.rb
models 'rails_helper'

RSpec.describe Item, type: :model do
  
   before do
    @item = FactoryBot.build(:item)
   end
   describe '商品出品機能' do
  it "全て正常" do
    # @item.image = nil
    expect(@item).to be_valid
  end

  it 'imageが必須であること' do
    @item.image = ""#nil
    @item.valid?
    expect(@item.errors.full_messages).to include("Image can't be blank")
  end

  it `id必須であること` do


  it "商品名が必須であること" do
    @item.name  = ""
    @item.valid?
    binding.pry
    expect(@item.errors.full_messages).to include ""
  end
end
end

前提として単体テストコードは文字入力した際にエラーを出すために行う。

   it "全て正常" do
    # @item.image = nil
    expect(@item).to be_valid
  end

この記述はこのテストは全て正常に動くように宣言している。この宣言がないと、テストコードがうまくいかなくなる。


  it 'imageが必須であること' do
    @item.image = ""#nil
    @item.valid?
    expect(@item.errors.full_messages).to include("Image can't be blank")
  end

imageが必須であること この記述はコンパクトにシンプルにまとめた。本来は 商品画像を1枚つけることが必須であること とあったが、アプリの動作を軽く&見やすくする為に、文言はシンプルに書く。
@image.image = ""と@image.image = nilは同じ意味である。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?