Michael Hartl のチュートリアルから、このテストについて質問があります。
モデル:
class User < ActiveRecord::Base
  .
  .
  .
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: true
end
テスト:
require 'spec_helper'
describe User do
  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
end
電子メールの一意性バリデーターについての私の理解では、データベースに 2 回追加することはできません。ただし、このテストでは、ユーザーは create ではなく new でインスタンス化されているだけです。
だからここに私が思うことが起こる:
@user = User.new(ただの記憶)- ...
 user_with_same_email = @user.dupメモリには 2 人のユーザーがいます- ...
 user_with_same_email.saveデータベースに最初のユーザーを挿入しているので、有効である必要がありますが、テストit { should_not be_valid }はパスします。
私は何を間違えていますか?