Listing 6.20 of the Michael Hartl rail tutorial shows the following code:
before do @user = User.new(name: "Example User", email: " user@example.com ") end . . . describe "when email address is already taken" do before do user_with_same_email = @user.dup user_with_same_email.email = @user.email.upcase user_with_same_email.save end it { should_not be_valid } end
I had a problem understanding this concept because @ user.dup returns a view of the same object that is copied to user_with_same, but @user was never saved to the database anywhere in the file. Therefore, the user_with_same_email.save test must be valid every time. However, the test passes. Someone please explain this ... is there an implicit database at @user = User.new (...)? I know if it was User.create (...), there would be a save, but not for the new method. Thanks!
source share