Rspec、shoulda、validate_uniqueness_of 以及范围和错误错误消息

发布于 2024-11-18 18:36:56 字数 1235 浏览 5 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小霸王臭丫头 2024-11-25 18:36:56

要检查 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)}

它会解决尝试一下。

To check validate_uniqueness_of(:field) but for this you should at lease one record in database to check uniquness constrain.
Here is....

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

To validate uniqueness

it { should validate_uniqueness_of(:country_code)}

It will work out try it out.

一曲琵琶半遮面シ 2024-11-25 18:36:56

添加到 Riche 所说的有关 :direction_down 的唯一性验证的内容,我怀疑您的 ProductLimit 工厂为 生成值的方式:方向_向下。它可能并不总是为所有具有唯一性验证的属性生成唯一值。

另外,我在唯一性验证方面遇到的一个问题是在验证检查之前创建的第一个对象(您的情况下的主题)不应与工厂“随机”生成的值有任何冲突的值。用一个简单的例子来说明,

Factory(:product_limit, :direction_down => "abc")
it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down) }

当 :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,

Factory(:product_limit, :direction_down => "abc")
it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down) }

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.

离鸿 2024-11-25 18:36:56

sepc_helper 中有database_cleaner 设置吗?
如果没有则添加

gem "database_cleaner"

到spec_helper.rb
RSpec.configure 块中添加以下内容

 config.use_transactional_fixtures = false

 config.before(:suite) do
   DatabaseCleaner.strategy = :truncation
 end

 config.before(:each) do
   DatabaseCleaner.start
 end

 config.after(:each) do
   DatabaseCleaner.clean
 end

另外您可以发布工厂代码吗?只是为了有一个更清晰的图片。

我希望这有帮助。

另请检查 :direction_down 属性是否有唯一性验证。

Do you have the database_cleaner settings in the sepc_helper?
if not then add

gem "database_cleaner"

in spec_helper.rb
add the following in the RSpec.configure block

 config.use_transactional_fixtures = false

 config.before(:suite) do
   DatabaseCleaner.strategy = :truncation
 end

 config.before(:each) do
   DatabaseCleaner.start
 end

 config.after(:each) do
   DatabaseCleaner.clean
 end

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.

新一帅帅 2024-11-25 18:36:56

尝试删除 test/fixtures/* 并尝试在测试唯一性之前手动创建对象(不使用 Factory)。另外,您是否尝试过:

    class MyModelTest < ActiveSupport::TestCase 

Try to remove test/fixtures/* and try to manually create object before testing uniqueness (not using Factory). Also, have you tried:

    class MyModelTest < ActiveSupport::TestCase 
浅唱ヾ落雨殇 2024-11-25 18:36:56

该错误对于匹配器来说是不正确的,因为当您要求它测试 :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.

所谓喜欢 2024-11-25 18:36:56

第一个案例(使用 validate_uniquess_of)发生在我意外崩溃后。一个简单的rake db:test:prepare修复了它。

The first case (with validate_uniquess_of) happened to me after an unexpected crash. A simple rake db:test:prepare fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文