I did a Google+ hangout/interview with +Victor Goff who taught me how to use Ruby DocTest. It reminded me of RSpec, but the key difference is that Ruby DocTest is used with IRB returns. Nifty, right?
Of course, I had to install a new gem. You can install it by running:
gem install rubydoctest
Victor created a file, hello.rb which contains code in a comment block. By running the following command, we ran an IRB session in the code comments:
This is what we created:
The => line is the 'expectation, or the assertion, or the 'should be' line as it would be for RSpec.
I learned that upon the creation of a new test that causes other tests to fail, it's best to ignore the older tests and work on the new feature until it passes.
On a side note, unrelated to Ruby DocTest, I also learned how to use a default param in Ruby. I had already known what it was, but this was the first time I got to see it in action. :P
When teaching new programmers, and new programmers to Ruby, they are going to use IRB to do their testing anyway. Why have the RSpec/minitest/test-unit framework overhead to learning? You can IMMEDIATELY get them thinking about how their methods return things, and testing, and measuring... with 0 additional overhead in learning. Also, it shows them that documentation and examples in their own programs is easy to do, and it doesn't get 'stale' when it is executable. They learn that they can safely refactor under test.
The advantage to having documentation would be that you can use rdoc to document your methods (and it is testable) and it also serves as example usage for those using your documentation.
In Real Life... Ruby DocTest for what I use it for is small snippets and portions of programs that I may not understand the domain well enough to actually write tests first. The IRB session right there let's me get feedback from my program... (And I don't necessarily have to open up an IRB session).
Victor Goff writes for RubyLearning.org where programmers can take courses to further their Ruby (and general programming) education. I met Victor on Google+ and while I've not participated in his RubyLearning courses, they appear to be very interesting and educational. (Definitely worth checking out.)