これは何?
SSIA
確認環境
$ bundle exec ruby --version
ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-darwin19]
$ bundle exec rails --version
Rails 6.0.4.6
確認手順
ファイル・データ準備
Gemfile
group :development, :test do
...
gem 'rspec-rails', '~> 4.0.1'
gem 'factory_bot_rails'
end
$ bundle exec rails g model Hoge
db/migrate/20220816164836_create_hoges.rb
class CreateHoges < ActiveRecord::Migration[6.0]
def change
create_table :hoges do |t|
t.string :col1, null: false
t.timestamps
end
end
end
$ rake db:migrate
spec/factories/hoges.rb
FactoryBot.define do
factory :hoge do
sequence(:col1) { |n| "no_#{n}" }
end
end
spec/models/hoge_spec.rb
require 'rails_helper'
RSpec.describe Hoge, type: :model do
it do
# NOTE: create_list で呼び出すようにする設定をしたい
FactoryBot.create_list(:hoge, 3)
p Hoge.all
end
end
実行結果の確認
$ rspec spec/models/hoge_spec.rb
#<ActiveRecord::Relation [#<Hoge id: 1, col1: "no_1", created_at: "2022-08-16 17:07:27", updated_at: "2022-08-16 17:07:27">, #<Hoge id: 2, col1: "no_2", created_at: "2022-08-16 17:07:27", updated_at: "2022-08-16 17:07:27">, #<Hoge id: 3, col1: "no_3", created_at: "2022-08-16 17:07:27", updated_at: "2022-08-16 17:07:27">]>
.
Finished in 0.02931 seconds (files took 1.31 seconds to load)
1 example, 0 failures