Rake Task for BDD Docs
We find that the pattern ‘test_should_***_on_***’ a useful way of naming tests – it’s an idea stolen from JBehave . If this test were to fail, I’m reminded that I should (!) ask myself the question ‘should the class I’m testing do this?’ before I go on a bug-hunt. The other advantage is that I can use my test classes to generate some simple documentation for my classes. Here’s a rake target that can do just that:
1 2 desc "Generate agiledox-like documentation for tests" 3 task :agiledox do 4 tests = FileList['test/**/*_test.rb'] 5 tests.each do |file| 6 m = %r".*/([^/].*)_test.rb".match(file) 7 puts m[1]+" should:\n" 8 test_definitions = File::readlines(file).select {|line| line =~ /.*def test.*/} 9 test_definitions.each do |definition| 10 m = %r"test_(should_)?(.*)".match(definition) 11 puts " - "+m[2].gsub(/_/," ") 12 end 13 puts "\n" 14 end 15 end
An example from our codebase, typing rake agiledox generates:
security_controller should:
- redirect to page stored in session on successful login
- store user object in session on successful login
- redirect to page stored in session after signup
- store user object in session after signup
- reject signup when passwords do not match
- reject signup when login too short
- report both errors if passwords dont match and username too short
- not store user in session if password not correct on signup
- remain on login page if password not correct on signup
- remove user from session on log out