blog.mhartl | Michael Hartl's tech blog

2008-07-28

Running Rails tests with autotest (ZenTest) and RSpec

Filed under: RSpec, Ruby on Rails, autotest — mhartl @ 13:12

I recently ran into a problem with autotest (ZenTest) after upgrading to Rails 2.1 and RSpec 1.4.1. Solving it was annoying, so I hope I can save others some trouble. Here’s the problem:

With RSpec, autotest hangs

Before the upgrade, I could run my specs just fine using the plain autotest command, but after the upgrade autotest just hangs:

$ autotest
loading autotest/rails

This is on a system running Mac OS X Tiger (10.4), Rails 2.1.0, RSpec 1.4.1, and ZenTest 3.10.0. Strangely, my friend Long could run autotest fine on a virtually identical system (so you may not run into this problem), but for me this only increased the frustration. After much hand-wringing (and a lot of Google searching), I finally found a Rails Forum post with a solution:

$ RSPEC=true autotest

Then autotest runs normally.

Restoring the old RSpec/autotest behavior

To get the old behavior, you can include the RSPEC variable in your environment rather than putting it explicitly on the command line. For example, on a system running bash, export the RSPEC variable as follows:

file: ~/.bashrc

export RSPEC=true

Then source it:

$ . ~/.bashrc

Now autotest should run as before:

$ autotest

Voilà (I hope)!

UPDATE: Since making this post, I’ve learned that RSpec now ships with a program called autospec that solves the same problem; just run

$ autospec

and the specs should run as expected.

24 Comments »

  1. Rock on! Thanks.

    I switched to R2.1 and RSpec at the same time … my previous work had all been in R2.0.2 with Test::Unit. So I’d actually never seen autotest work with rails at all, and assumed it must require some arcane configuration to get the two working.

    Given all the weird things people recommend you do (like installing a whole separate plugin just to run rspec with autotest, or separate ‘autospec’ commands), Googling this issue was entirely unhelpful.

    Comment by Evan — 2008-07-28 @ 13:34

  2. Thanks a lot! Once again The World is safe :)

    Comment by Jacek Becela — 2008-07-31 @ 01:24

  3. @Evan, @Jacek: Glad to be of service!

    Comment by Michael Hartl — 2008-08-3 @ 22:30

  4. You save me! Thanks..

    Comment by sjoonk — 2008-08-14 @ 10:56

  5. Thanks, you just saved me a few hours frustration!

    Comment by Paul — 2008-09-30 @ 13:36

  6. Thanks! I know you saved me several hours of digging through autotest.

    Comment by Stephen Veit — 2008-10-2 @ 04:06

  7. Thank you so much :) I wish I had found this page earlier

    Comment by Shamu — 2008-10-8 @ 06:34

  8. I have (what I think is) a similar problem – autotest runs my tests, but it doesn’t look in the ’spec’ directory, only in ‘test.’ I’ve got the the RSPEC=true set, but can’t figure out how to tell it where to look.

    Comment by Marcus — 2008-10-14 @ 09:39

  9. I was about to give up on autotest . . . One more Google search and try the first new solution to this problem and that was it. Made the change in my .profile as you suggest, fired up autotest, and it worked! Thank you!

    Comment by Ken Riley — 2008-10-22 @ 19:12

  10. hey thanks inoshi… fixed similar issue for me on intrepid ubuntu

    Comment by glenn — 2008-10-24 @ 17:39

  11. Saved me a lot of time! Thank you. :)

    Comment by Damien — 2008-10-27 @ 21:16

  12. Thank you! :)

    Comment by Rc.if — 2008-10-29 @ 11:33

  13. Awesome. I was just about to give up…

    Comment by Damon Cali — 2008-11-5 @ 09:48

  14. Like everyone else, you saved me. Thanks.

    Comment by Jeff Rafter — 2008-11-8 @ 14:31

  15. It looks like you should also add AUTOTEST=true (to get the color back in your autotest). Some folks have also been hacking to just do this in their ENV["AUTOTEST"] and ENV["RSPEC"]

    Comment by Jeff Rafter — 2008-11-8 @ 15:03

  16. @Jeff: my autotest colors show up anyway, but perhaps some configurations do require the additional AUTOTEST environment variable you mention. Thanks for the note.

    Comment by mhartl — 2008-11-10 @ 18:40

  17. my project is rails version 2.0.2 and gem version 1.3.0

    when I run ‘autotest -rails’ in my project it work in first time but after that it not run when I change in test file.

    please help me.

    Comment by phuwanart — 2008-12-15 @ 20:31

  18. Thanks, this fixed the same problem on Cygwin aswell.

    Comment by Joshua — 2009-01-12 @ 19:06

  19. Thanks a lot! This solved my problem as well!

    Comment by Anders Lemke — 2009-03-19 @ 01:56

  20. Great tip … was already thinking of throwing my mac from the balcony ;)

    Comment by Jeroen — 2009-04-3 @ 12:42

  21. N.B. Since making the original post, I’ve learned that the autospec program solves basically the same problem.

    Comment by Michael Hartl — 2009-04-3 @ 16:11

  22. Thank You

    Comment by devender — 2009-09-1 @ 17:29

  23. Much thanks. You saved me a big headache.

    Comment by David — 2009-09-5 @ 10:20

  24. Thanks a lot for the post.

    Comment by jamal — 2009-12-9 @ 22:42


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.