Rspec、shoulda、validate_uniqueness_of 以及范围和错误错误消息
我有以下 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)"]
I have following Rspec test:
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
But I get following confusing error:
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)"]
Can you help me? I don't understand why this isn't working, because the error message seems to be correct?
EDIT:
This happens too in other situations as well:
# 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)"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要检查 validate_uniqueness_of(:field) 但为此,您应该至少在数据库中查找一条记录来检查唯一性约束。
这是......
为了验证唯一性
它会解决尝试一下。
To check validate_uniqueness_of(:field) but for this you should at lease one record in database to check uniquness constrain.
Here is....
To validate uniqueness
It will work out try it out.
添加到 Riche 所说的有关 :direction_down 的唯一性验证的内容,我怀疑您的 ProductLimit 工厂为 生成值的方式:方向_向下。它可能并不总是为所有具有唯一性验证的属性生成唯一值。
另外,我在唯一性验证方面遇到的一个问题是在验证检查之前创建的第一个对象(您的情况下的主题)不应与工厂“随机”生成的值有任何冲突的值。用一个简单的例子来说明,
当 :direction_down 上有唯一性验证时,如果由 shoulda 匹配器构造的对象以设置为“abc”的 Direction_down 结尾,则有可能错误地失败。
To add to what Riche has said about uniqueness validation on :direction_down, I would suspect the way your ProductLimit factory generates values for :direction_down. It might not always be generating unique values for all attributes which have the uniqueness validation on them.
Also, one issue I have faced with uniqueness validation is the first object ( the subject in your case) thats is created before the validation check should not have any conflicting values with ones the factory "randomly" generates. To illustrate with a trivial example,
has the potential to fail wrongly, in case the object constructed by the shoulda matcher ends with direction_down set to "abc" when there is a uniqueness validation on :direction_down.
sepc_helper 中有database_cleaner 设置吗?
如果没有则添加
到spec_helper.rb
在 RSpec.configure 块中添加以下内容
另外您可以发布工厂代码吗?只是为了有一个更清晰的图片。
我希望这有帮助。
另请检查 :direction_down 属性是否有唯一性验证。
Do you have the database_cleaner settings in the sepc_helper?
if not then add
in spec_helper.rb
add the following in the RSpec.configure block
Also can you post the Factory code? just to have a clearer picture.
I hope this helps.
Also check if there is any uniqueness validation on :direction_down attribute.
尝试删除
test/fixtures/*
并尝试在测试唯一性之前手动创建对象(不使用 Factory)。另外,您是否尝试过:Try to remove
test/fixtures/*
and try to manually create object before testing uniqueness (not using Factory). Also, have you tried:该错误对于匹配器来说是不正确的,因为当您要求它测试 :price_cents 的唯一性是否有效时,它会抱怨 :direction_down 的唯一性。
您的模型的代码是否包含
validates_uniqueness_of :direction_down
?如果是这样,那就可以解释该消息了。The error isn't correct for the matcher because it's complaining about the uniqueness of :direction_down when you asked it to test that the uniqueness of :price_cents was valid.
Does the code for your model include a
validates_uniqueness_of :direction_down
? If so, that would explain the message.第一个案例(使用
validate_uniquess_of
)发生在我意外崩溃后。一个简单的rake db:test:prepare
修复了它。The first case (with
validate_uniquess_of
) happened to me after an unexpected crash. A simplerake db:test:prepare
fixed it.