【发布时间】:2017-03-30 15:18:37
【问题描述】:
我有这个 Rails 模型:
class Profile < ActiveRecord::Base
validates :number_format, :inclusion => { :in => ["1,000.00", "1.000,00"] }
def number_format=(format)
self.currency_delimiter = format[1]
self.currency_separator = format[5]
end
def number_format
"1#{currency_delimiter}000#{currency_separator}00"
end
end
问题是当我用 RSpec 测试它时...
it "is invalid without a number_format" do
expect(FactoryGirl.build(:profile, :number_format => nil).errors_on(:number_format).size).to eq(1)
end
...我收到此错误:
1) 没有 number_format 的配置文件 number_format 无效 失败/错误:expect(FactoryGirl.build(:profile, :number_format => nil).errors_on(:number_format).size).to eq(1)
expected: 1 got: 0
这怎么可能?
我认为nil 不会得到验证,因为我采用了验证方法。
【问题讨论】:
-
你的模型真的有属性
number_format吗?我只看到一个名称相似的 setter 方法。是否有名为number_format的数据库列或具有该名称的方法? -
是的,还有一个getter方法。我刚刚在上面添加了它。但是,没有具有该名称的数据库列。
标签: ruby-on-rails ruby validation rspec