views:

84

answers:

3

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:

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. There's one difference…

A: 

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]. 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(...)

liwp
A: 

You may want to check out Shoulda It will make your tests even prettier and it has nested contexts and lots of useful macros.

Kamil Sarna
+4  A: 

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. In the end it's a matter of personal preference.

John Topley
Cool. I'm assuming Rails just hasn't modified the rake:stats code to account for the newer syntax, then.
Andrew
That's right. There's been a ticket open for it since last February: https://rails.lighthouseapp.com/projects/8994/tickets/1915-rake-stats-bad-method-count
John Topley