テストコード #1
テスト結果のログ表示
(.rspec) <!-- ログが詳細に表示される --> --format documentation
エラーメッセージを英語に
(spec/rails_helper.rb) I18n.locale = "en"
after アフターメソッド
文字通り、〜の後に動作させたい時
FactoryBot.define do factory :message do content {Faker::Lorem.sentence} # インスタンスがbuildされた後に after(:build) do |message| # 指定したパスのファイルを名前をつけて保存 # ioはinput/outputの略 # ファイルをフルパスにすると、他の環境では使えなくなるので注意 message.image.attach(io: File.open('public/images/test_image.png'), filename: 'test_image.png') end end end
attach
ファイルを紐づける。
外す時はpurge
by chatGPT
messageはアクティブレコードモデルのインスタンス。
imageはそのモデルに関連付けられたActive Storageアタッチメント名。アタッチメントとは、ファイルをモデルに関連付けて管理するための仕組み。
attached?
画像がattachできているか確認。
# 変数に格納 pry(main)> message = FactoryBot.create(:message) pry(main)> message.image.attached? => true
RSpecは外部ライブラリ
ライブラリの種類を学習してからだと、requireの意味がわかって少しおもしろ。
# 共通の設定とかメソッドを読み込むためのrequire require 'rails_helper'
(rails_helper内に、その下部ディレクトリを読み込む設定が書かれている。)
単体テストの基本
基本は、インスタンス生成とエクスペクテーション
rails g rspec:model user
先にFactoryBotのファイルを手作業で作ってるとconflictってなってる。なんかなるほど。
(conflict: 衝突)
(ターミナル) % rails g rspec:model user create spec/models/user_spec.rb invoke factory_bot conflict spec/factories/users.rb # 上書きしますか? Overwrite /Users/shigeshige/projects/chat-app/spec/factories/users.rb? (enter "h" for help) [Ynaqdhm]
([Ynaqdhm] : 返答方法) Y - yes, overwrite #上書する。 n - no, do not overwrite #上書きしない。 a - all, overwrite this and all others #全てを上書きする q - quit, abort #実行の中断をする d - diff, show the differences between the old and the new #ファイルを古いデータ、新しいデータで比較する? h - help, show this help #ヘルプを呼び出す m - merge, run merge tool #マージする
出典: (enter "h" for help) [Ynaqdhm]の対処方法 - Qiita
belongs_to :user の時に注意
外部キー(user_id)のバリデーションは不要!
class Message< ApplicationRecord belongs_to :user # ↑この時点で、':user_idの有無'というバリデーションはデフォルトで設定されている。 validates :user_id presence: true ←不要 # このバリデーションを設定した場合のエラーメッセージは、'User can’t be blank' # 設定しなかった場合は、'User must exist' end
click_on
click_on('hoge') # テキストリンク(a要素とか) or ボタン要素 をクリックしてくれる。 # テキストを検索 or value属性 を検索
英単語
invoke
in(上)とvocare(呼ぶ) -> 法令を発動する
- vocal: vocalis(声を出すこと) -> 声の
- vocation: 語源はvocatus(呼ばれる) -> 転職
- provoke: pro(前)とvocare(呼ぶ) -> 怒らせる
- revoke: re(後ろ)とvocare(呼ぶ) -> 取り消す
- advocate: ad(方向)とvocare(呼ぶ) -> 支持する
- convocation: con(一緒)とvocare(呼ぶ) -> 招集
- evoke: ex(外)とvocare(呼ぶ) -> 感情を引き起こす