如何删除 shoulda 测试中的重复项?
这就是我所得到的:
context "Create ingredient from string" do
context "1 cups butter" do
setup do
@ingredient = Ingredient.create(:ingredient_string => "1 cups butter")
end
should "return unit" do
assert_equal @ingredient.unit, 'cups'
end
should "return amount" do
assert_equal @ingredient.amount, 1.0
end
should "return name" do
assert_equal @ingredient.name, 'butter'
end
end
context "1 (18.25 ounce) package devil's food cake mix with pudding" do
setup do
@ingredient = Ingredient.create(:ingredient_string => "1 (18.25 ounce) package devil's food cake mix with pudding")
end
should "return unit" do
assert_equal @ingredient.unit, '(18.25 ounce) package'
end
should "return amount" do
assert_equal @ingredient.amount, 1.0
end
should "return name" do
assert_equal @ingredient.name, 'devil\'s food cake mix with pudding'
end
end
end
显然那里有很多重复。 关于如何删除它的任何想法,如果至少是上下文和字符串?
Here is what I have:
context "Create ingredient from string" do
context "1 cups butter" do
setup do
@ingredient = Ingredient.create(:ingredient_string => "1 cups butter")
end
should "return unit" do
assert_equal @ingredient.unit, 'cups'
end
should "return amount" do
assert_equal @ingredient.amount, 1.0
end
should "return name" do
assert_equal @ingredient.name, 'butter'
end
end
context "1 (18.25 ounce) package devil's food cake mix with pudding" do
setup do
@ingredient = Ingredient.create(:ingredient_string => "1 (18.25 ounce) package devil's food cake mix with pudding")
end
should "return unit" do
assert_equal @ingredient.unit, '(18.25 ounce) package'
end
should "return amount" do
assert_equal @ingredient.amount, 1.0
end
should "return name" do
assert_equal @ingredient.name, 'devil\'s food cake mix with pudding'
end
end
end
Clearly there is a lot of duplication there. Any thoughts on how to remove it, if only at the very least the context and the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
测试/规格不是生产代码,因此干燥不是首要任务。
原则是规范应该清晰易读,即使这意味着测试之间存在重复的文本。
不要太担心规格是否干燥。 过分强调干测试往往会让事情变得更加困难,因为你必须跳转到事物的定义才能理解正在发生的事情。
Tests/specs are not production code and so being dry is not a priority.
The principle is that the specs should be clear to read, even if it means there is duplication of text across tests.
Don't be too concerned about specs being dry. Overemphasis of dry tests tends to make things more difficult as you have to jump around to the definitions of things to understand what is happening.
测试中的重复不一定是一件坏事(tm)
我建议您阅读 Jay Field 的以下文章
http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html
http://blog.jayfields.com/2008/05/testing-duplicate-code-in -your-tests.html
他们为测试中的代码重复并为每个测试保留一个断言提供了令人信服的案例。
Duplication in tests is not necessarily a Bad Thing(tm)
I suggest you read the following articles from Jay Field
http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html
http://blog.jayfields.com/2008/05/testing-duplicate-code-in-your-tests.html
They make a convinving case for code duplication in the tests and keeping one assertion per test.
这是针对您的具体问题的解决方案。 这个想法是创建一个类方法(如 Shoulda 的 context、setup 和 should)。
将重复封装在一个类方法中,接受所有不同的部分作为参数,如下所示:
现在,您可以使用一个内衬调用所有这些封装的测试(这里使用 5 个内衬以提高可读性;-)
在某些情况下,您可能希望接受一个块,该块可以作为您的 Shoulda 设置。
Here's a solution to your specific problem. The idea is to create a class method (like Shoulda's context, setup and should).
Encapsulate the repetition in a class method accepting all varying parts as arguments like this:
Now you can call all these encapsulated tests with one liners (5-liners here for readability ;-)
In some cases, you may want to accept a block which could serve as your Shoulda setup.
就我个人而言,对于这个测试,我不会使用 Shoulda。
您可以使用动态方法创建轻松删除重复项,如下所示:
Personally for this test, I wouldn't use Shoulda.
You can easily remove duplication by using dynamic method creation as follows: