infoモデルのテスト
infoのスキーマ
class CreateInfos < ActiveRecord::Migration[7.1]
def change
create_table :infos do |t|
t.string :title, limit: 100
t.string :heading_text, limit: 1000
t.string :contents, limit: 1000
t.date :public_date
t.integer :public_type
t.string :pdf_path, limit: 100
t.string :link, limit: 100
t.integer :blank
t.timestamps
end
end
end
この中からmodelを使う。
deploy@ca80ad7856d3:/apps# rails g | grep rspec
rspec:channel
rspec:controller
rspec:feature
rspec:generator
rspec:helper
rspec:install
rspec:integration
rspec:job
rspec:mailbox
rspec:mailer
rspec:model
rspec:request
rspec:scaffold
rspec:system
rspec:view
rails g rspec:model info
結果
create spec/models/info_spec.rb
生成ファイル
require 'rails_helper'
RSpec.describe Info, type: :model do
info = Info.new(
title: "タイトル",
contents: "おしらせ",
heading_text: 'ヘッド',
public_date: '2011-10-10'
)
expect(info).to be_valid #userが有効(valid)ならパスする
end
factory に手動作成
spec/facotries/info.rb
FactoryBot.define do
factory :info do
title {'テストタイトル'}
heading_text {'お知らせ'}
contents {"お知らせです。\n テスト"}
public_date {"2024-04-05"}
# public_type
# pdf_path, limit: 100
# link, limit: 100
# blank
end
end
実行
rspec spec/models/info_spec.rb