Rails 3:测试期间出现重复的验证错误消息
我遇到了一些奇怪的验证行为:它重复了我的验证错误消息,并且我无法弄清楚是什么导致了它......它不会在 Rails 控制台中执行此操作。
这是我的手机型号的验证:
# phone.rb
validates :number, :length => { :minimum => 3 }
我的规格:
require 'spec_helper'
describe Phone do
it "requires a number" do
user = User.make!
@p = Phone.new(number:nil,user_id:user.id,type:2)
@p.valid?
puts @p.errors.inspect
@p.should have(1).error_on(:number)
end
我的测试结果:
# rspec and machinist
#<ActiveModel::Errors:0x000000036f1258 @base=#<Phone id: nil, user_id: 614, kind: nil, number: nil, created_at: nil, updated_at: nil>, @messages={:number=>["is too short (minimum is 3 characters)", "is too short (minimum is 3 characters)"]}>
F
Failures:
1) Phone requires a number
Failure/Error: @p.should have(1).error_on(:number)
expected 1 error on :number, got 2
# ./spec/models/phone_spec.rb:11:in `block (2 levels) in <top (required)>'
Finished in 0.50988 seconds
1 example, 1 failure
如您所见,我两次收到“太短(最少 3 个字符)”...这也是/仅/在测试期间发生。 有什么想法吗?
I'm getting some weird validation behavior: it's duplicating my validation error messages and I can't figure out what's causing it... it doesn't do this in the rails console.
Here is the validation from my Phone model:
# phone.rb
validates :number, :length => { :minimum => 3 }
My spec:
require 'spec_helper'
describe Phone do
it "requires a number" do
user = User.make!
@p = Phone.new(number:nil,user_id:user.id,type:2)
@p.valid?
puts @p.errors.inspect
@p.should have(1).error_on(:number)
end
My test results:
# rspec and machinist
#<ActiveModel::Errors:0x000000036f1258 @base=#<Phone id: nil, user_id: 614, kind: nil, number: nil, created_at: nil, updated_at: nil>, @messages={:number=>["is too short (minimum is 3 characters)", "is too short (minimum is 3 characters)"]}>
F
Failures:
1) Phone requires a number
Failure/Error: @p.should have(1).error_on(:number)
expected 1 error on :number, got 2
# ./spec/models/phone_spec.rb:11:in `block (2 levels) in <top (required)>'
Finished in 0.50988 seconds
1 example, 1 failure
As you can see, I'm getting "is too short (minimum is 3 characters)" twice... It's also /only/ happening during testing.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题出在以下行:
在spec_helper.rb 文件中,在Spork.each_run 块中
,如果将方法“load”更改为“require”,则可以解决问题。
或者,如果您有足够新的 Spork 版本,则可以完全删除该行。
我非常确定该错误是当有人使用旧指令安装较新版本的 Spork(0.9.0+) 时引起的,因为该行:
甚至不再需要在 spec_helper.rb 文件中明确说明。如果是这样,那么当在spec_helper.rb文件中使用load方法时,它会重新加载指定的文件,这很可能是奇怪的RSpec重复验证错误的原因。
The problem is in the line:
in the spec_helper.rb file, in the Spork.each_run block
If you change the method 'load' to 'require', it fixes the problem.
Or if you have a recent enough version of Spork, you can remove the line altogether.
I am pretty sure the error is caused when someone is installing a newer version Spork(0.9.0+) with old instructions, because the line:
doesn't even has to be stated explicitly in the spec_helper.rb file anymore. If it is then when the load method is used in the spec_helper.rb file, it reloads the files specified , most likely the cause of the strange RSpec duplicate validations errors.
我遇到了类似的重复错误消息问题,但这似乎源于我使用了与标准不同的目录结构,例如:
我的
load
/require
调用Spork.each_run
块看起来像这样:我删除了它并将其替换为以下内容:
并且不再有重复的错误消息。
这篇文章对我有帮助: http:// /adams.co.tt/blog/2012/04/12/duplicate-active-model-validation-errors/ 其中作者说这是一个 1.8.7 特定的问题这涉及到需要解析为同一文件的不同路径,但我使用的是 1.9.3,因此它可能不相关。
I encountered a similar issue of duplicate error messages, but it seemed to stem from my use of a different directory structure than the standard, e.g.:
My
load
/require
call in theSpork.each_run
block looked like this:I removed this and replaced it with these:
And there were no more duplicate error messages.
I was helped by this post: http://adams.co.tt/blog/2012/04/12/duplicate-active-model-validation-errors/ in which the author says it's a 1.8.7-specific problem which involves requiring different paths which resolve to the same file, but I am using 1.9.3 so it may not be related.
我不确定这是否能解决您的问题,但如果您不遵循 rspec 约定,则 rspec 会非常奇怪。尝试一些更惯用的 rspec,如下所示:
require 'spec_helper'
我还建议您不要对 Rails 内置的验证进行单元测试;它们已经在 Rails 本身中进行了测试。具体来说,您的长度验证在 activemodel/test 中进行测试/cases/validations/length_validation_test.rb。验证的行为应该包含在集成测试中。
我希望这有帮助。
I'm not sure if this is going to solve your problem, but rspec is very quirky if you don't follow rspec convention. Try some more idiomatic rspec like the following:
require 'spec_helper'
I'd also like to suggest that you shouldn't unit test Rails' built in validations; they are already tested in Rails itself. Specifically, your length validation is tested in activemodel/test/cases/validations/length_validation_test.rb. The behavior of the validations should be covered in your integration tests.
I hope that's helpful.
我遇到了同样的问题(使用 rspec 和 spork)。
我怀疑这与模型被需要两次有关,导致验证运行两次。
如果您在规范顶部明确
require 'phone'
,它似乎可以修复它。但我真的很想知道是什么导致了这个问题......
I'm having the same problem (using rspec & spork).
I suspect it's something to do with the model being required twice, causing validations to run twice.
If you explicity
require 'phone'
at the top of your spec, it seems to fix it.But I'd really like to know what's causing the problem...