对不起我的英文,
我的模型验证
validates_uniqueness_of :price, scope: [:brand, :establishment, :presentation, :user], conditions: -> { where(created_at: Date.today.beginning_of_day..Date.today.end_of_day) }
这是我的工厂并测试它。
FactoryGirl.define do
factory :price do
association :establishment
association :presentation
association :brand
association :user
price 9.99
end
end
it "is invalid on duplicated by date" do
price = create(:price)
expect(build(:price, price.attributes)).to have(1).errors
end
我正在......
1) Price is invalid on duplicated by date
Failure/Error: expect(build(:price, price.attributes)).to have(1).errors
expected 1 errors, got 0
为什么吗
答案 0 :(得分:0)
仅当您在对象上调用valid?
时,验证错误才会添加到新对象(未保存)
it "is invalid on duplicated by date" do
price = create(:price)
new_price = build(:price, price.attributes)
new_price.valid?
expect(new_price).to have(1).errors
end
答案 1 :(得分:0)
我解决了,我的错误是在数据库概念上,我有一个浮动类型的价格字段,我把它改为十进制类型来修复错误。