gpt4 book ai didi

ruby-on-rails - Rspec, shoulda, validate_uniqueness_of 与范围和错误的错误信息

转载 作者:行者123 更新时间:2023-12-03 20:39:52 25 4
gpt4 key购买 nike

我有以下 Rspec 测试:

describe Productlimit do

before(:each) do
@productlimit = Factory.create(:productlimit, :user => Factory.create(:user))
end

subject { @productlimit }

...

it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
...
end

但我收到以下令人困惑的错误:
1) Productlimit 
Failure/Error: it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
Expected errors to include "has already been taken" when price_cents is set to 9530, got errors: ["direction_down has already been taken (false)"]

你能帮助我吗?我不明白为什么这不起作用,因为错误消息似乎是正确的?

编辑:

在其他情况下也会发生这种情况:
# product_spec.rb
...
it { should validate_numericality_of(:price).with_message("price_cents must be greater than 0 (0)") }

# rake spec:models
Failure/Error: it { should validate_numericality_of(:price).with_message("price_cents must be greater than 0 (0)") }
Expected errors to include "price_cents must be greater than 0 (0)" when price is set to "abcd", got errors: ["price_cents must be greater than 0 (0)"]

最佳答案

要检查 validate_uniqueness_of(:field) 但为此,您应该至少在数据库中有一条记录来检查唯一性约束。
这是....

before do
@country = FactoryGirl.create(:country, :id =>"a0000007-0000-0000-0000-000000000009",:currency_id => "a0000006-0000-0000-0000-000000000004",:merchant_id => "a0000001-0000-0000-0000-000000000002",:country_code => 'CAN', :name => 'Canada')
end

验证唯一性
it { should validate_uniqueness_of(:country_code)}

它会解决的。

关于ruby-on-rails - Rspec, shoulda, validate_uniqueness_of 与范围和错误的错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6564116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com