A Semi-Intelligent Watchr Script For Rails And RSpec

I’m using Watchr in my current Rails 3 project, instead of Autotest, to run my Cucumber and RSpec tests whenever I save a file. It makes life so much easier than having to manually run them. Yesterday, I decided to put a little more intelligence in my rspec script, and have it run the tests for the file that i’m working with – whether it’s a spec file or a model, controller, or whatever else.

Here’s what I came up with as a starting point:

Run it via “watchr rspec.watchr” from the command line / terminal. The first thing it does is run all specs in the /spec folder – a quick sanity check or smoke test to make sure everything is working before you start on new specs or code changes.

 

What It Does

When you save any file in the /app folder – whether it’s a model, a controller, sass file, or anything else anywhere under that folder, it will try to find the corresponding _spec file in the /spec folder. For example, if you save /app/models/user.rb it will try to find the /spec/models/user_spec.rb specs, and run them.

Running Specs: /spec/models/user_spec.rb

Followed by the usual RSpec output. If the spec file that it is looking for is not found, it tells you which file was saved and what _spec.rb file it was expecting to find.

Specs Not Found For:
/app/controllers/my_controller.rb

Looking For Spec File:
/spec/controllers/my_controller_spec.rb

It searches as deep as you want to go within your models folder, as well. If you have a model at

/app/models/some/folder/name/my_model.rb

then the script will attempt to run specs at

/spec/models/some/folder/name/my_model_spec.rb

And lastly, whenever you save a file in the /spec folder directly, it runs that file.

 

It will Probably Do More, In The Future

It’s fairly basic, but it gets the job done for now. I’m sure I’ll add to it over time, too. What features do you have in your watchr script? What am I missing that you would like to see? I have a few ideas, but I’m not sure if they are feasible, yet.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Productivity, RSpec, Ruby, Smoke Test, Test Automation, Testing, Unit Testing. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://craigjolicoeur.com Craig P Jolicoeur

    Thanks for the watchr script.

    Anyway you know of to have this run the full spec test suite after a test recovery like autotest does? (Passing test file following a previous failure)

  • http://www.lostechies.com/members/derick.bailey/default.aspx derick.bailey

    I’m sure it could be done, but don’t have the complete detail on how to do it, off-hand.

    my guess would be to set up some STDOUT scraping so that you can check for failures and log them. then use a little logic to track when the last failures were and wait for current tests to go green, then re-run entire suite.

    shouldn’t be too terribly difficult to do. would be a great addition to the script, too. maybe i’ll look into that when i have a chance. :)