初心者です。記念すべき1記事目です。
バリデーションのテストコードを書いていたのですが、ふと、どうやったら元々のエラーメッセージを変えれるんやと思いまして、色々調べたところ以下の書き方で上手く行ったので共有しておきます。
最初
元々のエラー文を前提としたテストコード
it "is invalid if not having uniqe email" do
user = create(:user)
user2 = build(:user)
user2.valid?
expect(user2.errors[:email]).to include("has already been taken")
end
it "is invalid without a password" do
user = build(:user, password: "", password_confirmation: "")
user.valid?
expect(user.errors[:password]).to include("can't be blank")
end
テスト結果
en.ymlを書き換える
en:
activerecord:
errors:
messages:
blank: "what the fuck!!! You need to fill it"
taken: "what the hell are you thinking? it already exists"
hello: "Hello world"
テスト結果
以上です