12

I happen to be a subscriber of Ruby Inside, since I'm particularly interested in Rails. Yesterday, the creator of Rails, David Heinemeier Hansson, pretty much said that he's just using test/unit. I would understand that, since it's Rails internal, but he seems to have given a strong opinion. He believes that RSpec and Cucumber are needlessly complicated.

I would normally not pay much attention, but it depends on who says something. I respect Hansson a lot and his opinion got me thinking. When I started with Rails, I never really looked into test/unit. Just RSpec and Cucumber.

And that is why I want your insight. Do you think that RSpec is indeed complicated for not much added value? Does writing test/unit take less time and effort?

2
  • I agree with DHH because rails test/unit looks good. I have used rails testing frame work, rspec, selenium etc and feel the same as DHH
    – Ashish
    Mar 30, 2011 at 7:29
  • 1
    if you have load of resources plus money plus time 50% more. Needless to say relatively frozen requirements BDD/TDD is great otherwise stick to what DHH said. So thankful he wrote this blog post..
    – Abs
    May 16, 2014 at 19:43

3 Answers 3

14

My recommendation would be to use either Shoulda (extends Test::Unit) or RSpec with Capybara, and -no- Cucumber.

I think that the use of either RSpec or Shoulda for nested contexts is definitely worth doing. RSpec is definitely heavy-weight (perhaps overweight) though, and I'm on the fence with it for that reason.

Cucumber, I've finally come to understand, is waaay more cumbersome than it's usually worth. You can accomplish what you need more simply and robustly with plain ol' integration tests and Capybara. Remember -- Capybara != Cucumber, and Capybara is quite capable all on its own.

Shoulda is nice, because it simply adds conveniences to the standard Test::Unit framework, and is therefore much lighter-weight than RSpec (technically, each solves a different set of problems, but they both provide nested-context capabilities). RSpec has the advantage of making assertions read more naturally, and also generating more helpful failure messages in many cases, without the need for writing message arguments on the assertions.

Also, remember that Cucumber does not actually require RSpec, so if you want to keep using Cucumber, you can do that with just Test::Unit. Choices abound.

2
  • Indeed, the automagic failure messages that come for free from rpsec are a huge part of it's allure. Mar 30, 2011 at 14:16
  • Since I wrote that post, I have learned that Cucumber does not need to be so cumbersome. It is not right for all projects, but it can be a very valuable way to codify specifications. The key to writing good Cucumber tests is to write them in terms of business value and avoid reverence to the specific implementation. If your Gherkin contains words like "page" or "click" that's not so great. Also, Cucumber tests do NOT necessarily need to be full stack. Cucumber tests might well execute at a lower level with a few integration tests working through Capybara for full-stack verification. Aug 18, 2013 at 11:00
8

It's all about semantics. RSpec and Test::Unit are similar functionally. Personally I've always preferred RSpec because I found it more natural to write tests using it. I also like the simplicity of writing custom matchers, and the default matchers provided are useful.

Cucumber is a different beast entirely. Yes it's fairly cumbersome and becomes hard to maintain if you don't organise your step definitions properly but it does have one very strong use case and that is when your client is writing your specifications.

I've worked on projects where the client has been writing Cucumber scenarios together with one of our QA team. As a non-technical person it's an extremely approachable and natural way to specify user stories in the code. Cucumber really helped us walk the walk when it came to following our agile practices. The quality of the end product benefitted from that but no I do not like Cucumber as a developer :)

3

It's a matter of personal taste.

I like to write easy cucumber tests without worrying about details. Just testing the "happy" paths of my app. (portable, understandable, slow)

I leave the details to Test/Unit. (easy, fast)

It takes more time to understand:

get :products, :session => @session_id_for_product_banana
assert_select "table" do
  assert_select "td#name", "Banana"
end

instead of

When I go to the banana page
Then I should see "Banana"

Sure those tests are not equal but who cares whether "Banana" is in a div or a table or doesn't have the right html-id.

I dont' like functional tests because after refactoring, the id's could be gone, the session expectation could be changed. If that's the case you will need to refactor your code AND tests. If you'd use cucumber you wouldn't have to change your scenario's.

2
  • 2
    What if you do care about the "Banana" being in the table, instead of being just part of the error message "undefined method 'find' for 'Banana':String" ?
    – Arsen7
    Jun 1, 2011 at 10:29
  • That could happen but it's not likely. It's far more likely that the id changes or that you decide to use a div. If the view doesn't contain the logic then normally you will come across this kind of error in a unit test. Also Cucumber tends too blow up with a stack trace on a runtime error. Jun 1, 2011 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.