LoginSignup
2
2

More than 3 years have passed since last update.

RSpec2回目商品出品機能テスト

Last updated at Posted at 2021-04-17

itemモデルのバリデーションを書こうと思います。

1)bundle install(済)
2)rails g rspec:install(済)
3).rspec・rspecフォルダ・spec/spec_helper.rb・rails_helper.rbの各フォルダあり。(済)

4)itemモデルのテストファイル作成(ここから)

ここでエラー発生

terminal
maedatakuo@maedatakudainoMacBook-Air furima-32844 % rails g rspec:model item
Running via Spring preloader in process 16513
   identical  spec/models/item_spec.rb
      invoke  factory_bot
   identical    spec/factories/items.rb

に怒られました。

解答

RspecとFactoryBotがインストールされるとモデルを作るたびにファイルが自動生成されているようでした。

Userの時のデータを活用しながらテスト作り再開

疑問1 ActiveHashの1〜6までのデータで登録するんだけどFakerで数字限定で乱数出せる?

確認できました

参考

https://www.rubydoc.info/github/stympy/faker/Faker%2FNumber.between

ruby
Faker::Number.between(from: 1, to: 10) #=> 7

疑問2 ActiveImageを使いながらFakerやFactoryBotで画像をアップロードできるのか?

@maca12velさん ありがとうございます。参考にさせていただきました。

https://qiita.com/maca12vel/items/ee4d16827f24f69080ae

*私の場合は「image」だったので@Katyaさん(@katya_swaさん)の使用している「picture」は「image」に変更しています。

①fixturesディレクトリの作成

20210417-163241.png

1-1.spec内にfixturesディレクトリがない場合は、作成する。

1-2.使いたい画像ファイルをfixturesディレクトリの中に入れる。

1-3.spec/fixturesの中に20210327-085606.pngという画像ファイルを入れています。

1-4.以下を記述

spec/models/item.rb
require 'rails_helper'

RSpec.describe Item, type: :model do
    describe '#create' do
      before do
        @item = FactoryBot.build(:item)
        @item.image = fixture_file_upload("/20210327-085606.png")
      end

疑問3 imageのテストが通らない

Item
  #create
    image,item_name,description,category_id,status_id,prefecture_id,delivery_fee_payment_id,delivery_prepare_id,priceの値が存在すれば登録できること
    imageが空では登録できないこと (FAILED - 1)
    item_nnameが空では登録できないこと
    descriptionが空では登録できないこと
    catego_idが空では登録できないこと
    status_idが空では登録できないこと
    delivery_fee_payment_idが空では登録できないこと
    delivery_prepare_idが空では登録できないこと
    prefecture_idが空では登録できないこと
    prefecture_idが空では登録できないこと
    priceは全角カナでは登録できないこと

Failures:

  1) Item#create imageが空では登録できないこと
     Failure/Error: @item.valid?

     ActiveSupport::MessageVerifier::InvalidSignature:
       ActiveSupport::MessageVerifier::InvalidSignature
     # ./spec/models/item_spec.rb:16:in `block (3 levels) in <top (required)>'

Finished in 0.24549 seconds (files took 1.21 seconds to load)
11 examples, 1 failure

Failed examples:

rspec ./spec/models/item_spec.rb:14 # Item#create imageが空では登録できないこと

ただいま調査中

結論からいうとActive Strageの使用でからという表現を””ではなくnilとしないといけないらしい

(誤)

spec/models/item.rb
      it 'imageが空では登録できないこと' do
        @item.image = ""
        @item.valid?
        expect(@item.errors.full_messages).to include("Image can't be blank")
      end

(正)

spec/models/item.rb
      it 'imageが空では登録できないこと' do
        @item.image = nil
        @item.valid?
        expect(@item.errors.full_messages).to include("Image can't be blank")
      end

参考:@a1b2c3d4e5f1111さんありがとうございます。参考にします。

https://qiita.com/a1b2c3d4e5f1111/items/85817cdb8ca8544b88ea

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