我有以下Rspec的測試:Rspec的,早該,validate_uniqueness_of與範圍和錯誤的錯誤消息
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)"]
你正在使用什麼版本的shoulda/rspec? – nathanvda
我有shoulda-matchers(1.0.0.beta2),但我用當前的'shoulda'寶石試了一下。 – Lichtamberg
在我看來,你正在測試rails應該已經測試過的東西。如果你的模型中有validate_numericality,爲什麼還要在你的測試中調用它?這是不必要的重複 – corroded