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.

【RSpec】日付連番のデータを大量に作成したい

Last updated at Posted at 2021-10-26

##環境
Ruby 3.0.2
Rails 6.1.4.1

##成功
前提:userは定義済み、Postテーブルにdateカラムが存在する。
dateのループの中でデータ作成する

spec.rb
(Date.new(2021, 1, 1)..Date.new(2021, 9, 1)).each do |date|
   create(:post, date: date, user: user)
end

##失敗

FactoryBotのdateカラムを定義する際にdateをループして

factory.rb
FactoryBot.define do
  factory :post do
    date { 
      (Date.new(2021, 1, 1)..Date.new(2021, 9, 1)).each do |date|
        date
      end
    }
  end
end

テストの方で200個作成

spec.rb
...
create_list(:post, 200, user: user)
...

すると
「レンジを引用できない」エラー

 TypeError:
       can't quote Range
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?