[英]Ruby testing: block vs. method
As I've been writing test suites for a Rails app, I've noticed that there are two different ways to create tests, a test
block and a regular method: 由于我一直在为Rails应用程序编写测试套件,我注意到有两种不同的方法可以创建测试,
test
块和常规方法:
test "the truth" do
assert true
end
vs. 与
def test_for_something_else
assert true
end
Is there any difference between the two testing structures? 两个测试结构之间有什么区别吗? When should I use one over the other?
我什么时候应该使用另一个?
Edit: On further inspection, tests written as methods are counted in rake:stats
while those written with the test
syntax are not. 编辑:进一步检查时,作为方法编写的测试将计入
rake:stats
而使用test
语法编写的test
则不计算。 There's one difference… 有一个区别......
I think the block syntax was added due to the popularity of Behaviour-Driven-Development frameworks such as RSpec, with their more natural Should do X... syntax. 我认为块语法是由于行为驱动开发框架(如RSpec)的流行而增加的,它们更自然地应该做X ...语法。 In the end it's a matter of personal preference.
最后,这是个人偏好的问题。
I would expect that technically there is no difference between the two. 我希望技术上两者之间没有区别。 The former allows you to use a string to describe your test, whereas with the latter form you have to come up with a descriptive method name which are sometimes annoyingly long and hard to read [1].
前者允许您使用字符串来描述您的测试,而使用后者的形式,您必须提出一个描述性的方法名称,这些名称有时令人烦恼且难以阅读[1]。 Because of this difference I would personally prefer the former, but in the end it's down to personal / project preference.
由于这种差异,我个人更喜欢前者,但最终归结为个人/项目偏好。
[1] "add two interleaving timers that trigger in the opposite order"
vs. test_addTwoInterleavingTimersThatTriggerInTheOppositeOrder(...)
[1]
"add two interleaving timers that trigger in the opposite order"
与test_addTwoInterleavingTimersThatTriggerInTheOppositeOrder(...)
One difference/annoyance I have found is in Vim with Ctags and the TagList plugin. 我发现的一个区别/烦恼是Vim与Ctags和TagList插件。 If your tests are not written as functions you can't quickly browse them using the
:TlistToggle
command. 如果您的测试不是作为函数编写的,则无法使用
:TlistToggle
命令快速浏览它们。 If you have a lot of tests in one file this can make quickly navigating them more difficult. 如果您在一个文件中进行了大量测试,则可以快速导航它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.