0

我的课

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
4

1 回答 1

0

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

存在语法问题。你应该使用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

希望有帮助!!!

于 2013-09-16T10:05:44.013 回答