1

我有以下用户类

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

和我的 rspec 代码

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

引发以下错误

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

预期用户无效但有效

4

1 回答 1

0

user_with_same_email将无法保存,因此数据库中不会有第二个用户使用您的电子邮件;假设subject@user,不会有冲突并且用户将是有效的。

你忘了定义subjectuser_with_same_email

于 2013-09-16T08:37:59.623 回答