This is an absolute beginners question, it's probably pretty obvious but I couldn't find the answer yet.
I'm trying to validate an email address in Ruby on Rails using minitest.
test "should have format of email address" do
user = User.create(name: "Dummy", last_name: "Dummy", email: "example.com")
assert_match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, user.email)
assert_equal ["is invalid"], user.errors[:email]
end
The problem occurs in the last line assert_equal ["is invalid"], user.errors[:email]
.
The email address example.com
isn't matched by the regex on purpose since I want to check the error message "is invalid" against the one of the failed test but I can't figure out how to pass it into assert_equal
:
Error Message:
Expected /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i to match "example.com".
Should be:
'is invalid.'
Maybe I am wrong, but I my understanding was that error messages of the object could be found in object.errors[:symbol].