I'm about halfway through the Rails tutorial (excellent, btw), and have a little question. Is there a reason that this test uses duplicate_user.email = @user.email.upcase
and not the more succinct duplicate_user.email.upcase
?
Here is the full test.
test "email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.email = @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end
As far as I can tell, the test performs correctly doing it either way.
I'm not familiar with the tutorial but you seem to be asking why the test is not written as:
test "email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.email.upcase
@user.save
assert_not duplicate_user.valid?
end
In this case, the line duplicate_user.email.upcase
will just return the upcased email. It will not affect the attribute on the object. The test still passes because the duplicate_user
has the same email address as the original @user
, satisfying the test spec that email addresses should be unique.
What the test is actually doing is verifying that the code recognises an upcased email to be the same as a downcased email. In such a case, the original line in the test has the effect of assigning to the email
attribute of the duplicate_user
an upcased version of the email address.