Rails Tutorial Rspec misunderstanding

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!

+4
source share
1 answer

You are missing an implicit save.

user_with_same_email correctly saves (personally, I will always use save !, to make sure that he did not fail)

What spec specifies is that an object (i.e. @user ) cannot be saved due to the existence of a row in the database with the same email address.

+4
source

All Articles