Currently following the Michael Hartl rails tutorial
Given the following tests in rails
test "email validation should accept valid addresses" do
valid_addresses = %w[[email protected] [email protected] [email protected]
[email protected] [email protected]]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
test "email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
foo@bar_baz.com foo@bar+baz.com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end
and the following regex for email format validation
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
Can someone explain to me what the tests are testing with respect to the regex? Why are the valid tests only [email protected], [email protected], and so on. What if i add another element to valid_addresses that's [email protected]. Why did Michael specifically choose the above 5 example emails as valid_addresses and 5 invalid_addresses?
If the regex tests for all formats and only returns a specific one, why do we need to test at all?