Rspec与mongodb无法正常工作

时间:2013-09-16 08:22:25

标签: mongodb rspec ruby-on-rails-4 mongomapper

我有以下用户类

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

预期用户无效但有效

1 个答案:

答案 0 :(得分:0)

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

您是否忘了将subject定义为user_with_same_email