【发布时间】:2011-12-21 13:30:33
【问题描述】:
如果name 属性中有空格,我会尝试阻止保存记录。我正在使用包含 ActiveModel 的 Mongoid,因此它应该与 ActiveRecord 完全相同。
class Post
include Mongoid::Document
field :name, type: String
validates :name, presence: true, format: { :with => /\S/ }
end
这是我的规格。最后一个失败了,我不知道为什么。
describe Post do
describe "validations" do
# passes
it "should require a name" do
post = Post.new name: nil
post.should_not be_valid
end
# passes
it "should accept valid names" do
post = Post.new name: "hello-with-no-spaces"
post.should be_valid
end
# fails ?????
it "should reject invalid names" do
post = Post.new name: "hello with spaces"
post.should_not be_valid
end
end
end
【问题讨论】:
标签: ruby-on-rails regex ruby-on-rails-3 mongoid activemodel