Story Runner Debug Trick
May 4th, 2008
Before I found the plugin has_many_polymorph, in the article has_many_polymorphs for dummies I had been following the article The other side of polymorphic :through associations. However the code was not doing exactly what I wanted, and each time I made a change to the model I had to reload my console to test it. I found myself doing a lot of repetitive typing. When I decided to try out has_many_polymorph, I decided a better way to learn this plugin and do testing was with rspec's story runner.
Set up
Story runner comes with rspec and rspec_on_rails. See Testing with RSPec for setup details
When I installed RSpec into my project it generated some files for Story Runner under $project/stories
1 2 |
cd $project ruby script/generate rspec |
Create a story
A story is just a text file. As a convention end the file name with _story.rb and place in $project/stories
emacs $project/stories/display_article_with_content_story.rb |
See the section 'Story Runner - Testing Object Intergration' in Testing with RSPec for a screencast and articles on setting up Story Runner
I created the following story display_article_with_content_story.rb which loaded the repetetitive data I kept loading into the console. Then set it to brake into the debugger. I was then able to play around with the code to see how it worked.
See Ruby Debug for setting up the debugger and making it act more like the irb console.
Run the story
1 2 3 |
cd $project export RAILS_ENV=test && rake db:migrate ruby stories/display_article_with_content_story.rb |
It will now load all your settings to get you into the state were you can do some testing. It will then open the ruby debugger, where you can experiment by doing various tests, and check various settings.
The advantage of doing this instead of writing your tests directly into the story is that you might not be skilled enough to know what code you need yet to write your tests, and this method gives you instant feedback of what can and can not be done, and the type things you can test for.
May 20th, 2008 at 08:13 PM thanks