1

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.

JuliaV
  • 13
  • 2
  • Just like with any programming language there is more than one way to do things he could have just done `duplicate_user.email.upcase!` and it would have given the same result. He might have wanted to keep things congruent with the example of duplicating information from a given user rather than manipulating the object itself. This is just my thoughts on this. – thedevlpr Dec 10 '14 at 15:03
  • No, that wouldn't have changed the email in the db. – Mark Swardstrom Dec 10 '14 at 15:16

1 Answers1

0

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.

Robin Fisher
  • 1,454
  • 1
  • 13
  • 23