Railstutorial.org 验证存在测试
我一直在学习 railstutorial.org 上的教程,我对作者的代码部分感到有点困惑 - 6.2.1 验证存在。
在用户模型中,本教程添加了 validates :name, :presence =>;正确。够简单的。
当作者选择编写 rspec 测试时,他做了一些我认为有点奇怪的事情。
describe User do
before(:each) do
@attr = { :name => "Example User", :email => "[email protected]" }
end
.
.
.
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
end
当人们可以摆脱 :each 块语句并简单地写:
it "should require a name" do
no_name_user = User.new(:name => "", :email => "[email protected]")
no_name_user.should_not be_valid
end
我知道作者使用 @attr
时, 为什么要费尽心思将空白字符串合并到 @attr
> 变量来验证电子邮件地址是否存在,这也是他使用块语句的原因之一——对我来说,遵循第二个块引用的结构更有意义。尽管如此,我还是有一种感觉,我在这里缺少一些东西。
我想到的另一个解释是,当需要输入很多键时,使用 @attr 结构会有所帮助,而不是只输入姓名和电子邮件的相当简单的情况。
有人有任何意见吗?
I've been working through the tutorials at railstutorial.org, and I was a little stumped by the author's code for section -- 6.2.1 Validating presence.
In the user model, the tutorial adds validates :name, :presence => true
. Simple enough.
When the author chooses to write the rspec test, he does something that I thought was a little strange.
describe User do
before(:each) do
@attr = { :name => "Example User", :email => "[email protected]" }
end
.
.
.
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
end
Why go through the trouble to merge a blank string to @attr
when one could get rid of the :each block statement and simply write:
it "should require a name" do
no_name_user = User.new(:name => "", :email => "[email protected]")
no_name_user.should_not be_valid
end
I know that the author uses the @attr
variable to validate the presence of the email address as well, which is one indication as to why he used the block statement -- to me it makes more sense to follow the structure of the second block quote. Still, I have a feeling there is something that I'm missing here.
Another explanation that crossed my mind is that it helps to use the @attr
structure when there are lots of keys to be entered, as opposed to this rather simplistic case of only name and email.
Anyone have any input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,有一个标准属性映射可用于所有测试。当测试要求某个值不存在时,该值就会被删除。
就我个人而言,我不相信这是值得的,因为它有点混淆了事情(正如你发现的),但它就是这样。
It's so there's one standard attributes map that can be used across all the tests. When a test requires that a value isn't there, it's removed.
Personally, I wasn't convinced it was worth it as it sort of obfuscates things (as you discovered), but there it is.
重点是测试中只包含与测试用例相关的代码。测试用户在没有名称的情况下无效时,唯一相关的属性是名称属性。该测试不必了解有关电子邮件属性的任何信息。
假设您添加了对新字段是否存在的验证 - 您必须更新在没有该字段的情况下构建用户的每个测试。通过顶部的
attr
哈希,您只需将新字段弹出其中,所有测试都很好。创建用于测试的对象是一个很常见的问题,有很多解决方案,并且有很多关于哪种方法最好的讨论。我建议你去工厂看看。 Machinist 和 FactoryGirl 是与 Rails 配合良好的两个替代方案。
The point is to only have code relevant for the test case in the test. The only relevant attribute when testing that the user isn't valid without a name is the name attribute. That test should not have to know anything about the email attribute.
Let's say you add validation for the presence of a new field – you'd have to update every test where you build up a User without that field. With the
attr
hash at the top, you just pop the new field in there, and all your tests are fine.Creating objects for testing is a common enough problem that there are many solutions, and plenty of discussion about which way is best. I'd suggest you look into factories. Machinist and FactoryGirl are two alternatives that work great with Rails.