I love Guard. I’ve posted about it and used it a lot. But it’s got some problems right now with all the downstream gems and addons that I like.
Of course, a more elegant solution is to not pre-fork and use Gary Bernhardt’s (and others’) advice of extracting domain objects from rails and running tests over that. This is fine within plain old ruby objects but sometimes you need to load rake or factory_girl and that requires rails. So let’s assume you can’t isolate your tests into lib/ or extras/ and you need fast tests while loading your entire Rails environment. That’s where Guard and Autotest come in.
The newest Guard installs Listen which doesn’t work. So I had to back down a version of listen. Added gem ‘listen’, ‘0.4.7’ to my Gemfile to get rid of polling warning. 0.5.0 is latest but it has a problem right now (bug opened 2 days ago as of today, grr). All that is unrelated to the real problem.
Spork-minitest doesn’t work with guard. The issue is that spork-minitest doesn’t support any options like -r or -e that the guard is passing.
So let’s switch to autotest. But first, let’s look at my current configuration and I’ll show you what I had to change. This is what my Guardfile looked like with Guard on a Rails project:
Delete that whole Guardfile and let’s switch to autotest with spork and minitest.
This is what my Gemfile looked like with Guard:
This is what my Gemfile looks like when switched to Autotest:
It works great! As I said in the Gemfile comments:
sporkin one terminal
bundle exec autotest -cfin another terminal
` -c is for avoiding re-running rake test on red/green -f is for fast start to avoid running rake test on startup `
Save a file like products_controller_test.rb and watch it run. Save the app/controllers/products_controller.rb and watch products_controller_test.rb run.
The only problem is that it reacts a little bit slow because of a 1 second default sleep in Autotest. So this is my entire .autotest file which includes a hack to reduce that 1 second to 0 seconds. No impact to CPU and it reacts as fast as Guard did.
I plan on doing a pull request to address this problem. So watch for that.