Question

I have the following user class

class User
  include MongoMapper::Document 
  key :email, String, :unique => true
end

and my rspec code

before do
  @user = FactoryGirl.create(:user)
end

 describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email
      user_with_same_email.save
    end
    it { should_not be_valid }
  end

raises the following error

1) User when email address is already taken 
      Failure/Error: it { should_not be_valid }
       expected #<User _id: BSON::ObjectId('5236bec5ebe8660fcb00001a'), accepted_tender_ids: [], avatar: #<Avatar _id: BSON::ObjectId('5236bec3ebe8660fcb000001'), digest: "c7358a60a79905a7d4e3383ba905f1baaab278b06a6f751971344cb91763068f", image: "c7358a60a79905a7d4e3383ba905f1baaab278b06a6f751971344cb91763068f">, busy: false, completed_count: 0, completed_tender_ids: [], confirmed: true, dialogs: [], dislikes: 0, email: "maurice_langworth@treutel.com", role: "customer", subspecializations: [], type: "personal"> not to be valid

Expected user is not valid but it is valid

Was it helpful?

Solution

user_with_same_email will fail to save, so there will not be a second user in the database with your email; assuming that subject is @user, there will be no conflict and the user will be valid.

Did you forget to define subject as user_with_same_email?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top