Rspec不使用mongodb,mongomapper

时间:2013-09-16 09:52:02

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

我的班级

class Review
  include MongoMapper::EmbeddedDocument
  key :user_name, String, :require => true
  key :user_avatar, String, :require => true
end

Rspec代码

  it "when user name is not present" do
    blank = FactoryGirl.build(:review, user_name: nil)
    blank.should_not be_valid
  end

为什么会引发此错误?

 1) Review when user name is not present
         Failure/Error: blank.should_not be_valid
           expected #<Review _id: BSON::ObjectId('5236d401ebe86612fe000006'), user_avatar: "#<Avatar:0xb60083c>")> not to be valid

1 个答案:

答案 0 :(得分:0)

根据您提供的详细信息,我希望您将进行验证测试。所以问题是在模型级别没有检查验证。可能是这个answer会指导你。

存在语法问题。您应该使用required而不是require。所以现在新代码应该是这样的:

class Review
  include MongoMapper::EmbeddedDocument
  key :user_name, String, :required => true
  key :user_avatar, String, :required => true
end

请仔细阅读本文档以获取更多详细信息:

http://mongomapper.com/documentation/plugins/validations.html

希望有所帮助!!!