LoginSignup
1
1

More than 1 year has passed since last update.

単体テストコード:imageが必須であることのテストコードが通らない時の対処方法

Last updated at Posted at 2021-02-09
spec/models/item_spec.rb
it 'imageが必須であること' do
    @item.image = nil
    @item.valid?
    expect(@item.errors.full_messages).to include ("Image can't be blank")
  end

  it "登録名が必須であること" do
    @item.name  = ""
    binding.pry
    @item.valid?
    expect(@item.errors.full_messages).to include ("Name can't be blank")
  end

単体テストコードのimageテストでは、

@item.image = ""のダブルクオーテーションという概念がない。""の記述にすると、https://gyazo.com/b89f642c7e2adfd3cdab491aac65bb32
というエラーが現れる。

@item.image = nilnilに記述を変更すると、https://gyazo.com/a272f1b8e2c47e8e334b4c237207285a
となりテストが成功する。

結論:画像(image)が必須であること というテストコードには””の概念がないので、nilを使用すること。

1
1
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
1
1