rspec-2 and autotest
March 15th, 2010
[Updated on 17 March, 2010]
I just released rspec-2.0.0.beta.4 with support for autotest, among other enhancements. Autotest integration is going to be a bit different in rspec-2. We’re removing the autospec command, which did nothing but set an environment variable and call autotest.
In rspec-2, you’ll use the autotest command directly, but doing so requires a small bit of configuration. As of beta.4, you’ll have to do add this configuration manually. Just create an autotest directory in the root of your project, put the following statement in ./autotest/discover.rb:
Autotest.add_discovery { "rspec2" }
The final 2.0.0 release will include a generator (even for non-rails projects) that will add this for you.


March 16th, 2010 at 4:16 am
How much will RSpec 2 differ from what is being published in The RSpec Book? Is The RSpec Book going to include any discussion about RSpec 2? It would really suck if that book becomes out of date before it even gets published.
March 16th, 2010 at 11:01 am
We’re updating the book for rspec-2 and rails-3 before we go to print.
March 16th, 2010 at 4:18 pm
Hi David, Will that magical incantation work if I put it in my ~/.autotest file?
March 16th, 2010 at 11:06 pm
Not working here:
/Users/sobrinho/.rvm/rubies/ruby-1.9.1-p378/bin/ruby /Users/sobrinho/.rvm/gems/ruby-1.9.1-p378/gems/rspec-core-2.0.0.beta.4/bin/rspec /Users/sobrinho/Sites/sparta3/spec/models/content_spec.rb /Users/sobrinho/Sites/sparta3/spec/models/page_spec.rb /Users/sobrinho/Sites/sparta3/spec/models/post_spec.rb /Users/sobrinho/Sites/sparta3/spec/models/user_spec.rb -O spec/spec.opts /Users/sobrinho/.rvm/gems/ruby-1.9.1-p378/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/command_line_options.rb:22:in
parse': invalid option: -O (OptionParser::InvalidOption) from /Users/sobrinho/.rvm/gems/ruby-1.9.1-p378/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/command_line_options.rb:13:inparse’ from /Users/sobrinho/.rvm/gems/ruby-1.9.1-p378/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/runner.rb:29:inrun' from /Users/sobrinho/.rvm/gems/ruby-1.9.1-p378/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/runner.rb:13:inblock in autorun’March 17th, 2010 at 2:04 pm
@Daniel - no. Autotest loads the .autotest files after it loads up the Autotest class, so we need to tell it what Autotest class to load earlier in the process.
March 17th, 2010 at 2:12 pm
@Gabriel - the error tells you there is an invalid option in spec.opts - try getting rid of spec/spec.opts and you should be fine. Then run “rspec -h” to see the options that are available to you in rspec-2.
March 17th, 2010 at 11:16 pm
@David
My spec.opts doesn’t have -O option. See:
$ cat spec/spec.opts –color –format progress
I didn’t find the problem. Have anyone the same issue?
March 17th, 2010 at 11:18 pm
I deleted my spec.opts file and this works. Strange, I will look this.
March 17th, 2010 at 11:23 pm
I’m confused, hopefully it will be easy for you to clear this up for me. Where is the autotest/discover.rb file when Autotest and rspec are installed as gems? Adding the line of code to ~/.autotest did not seem to get my rspec test run.
Thanks!
March 18th, 2010 at 4:25 am
@Bill - Make an autotest directory in your project root, and put discover.rb in that.
Both ./.autotest and ~/.autotest are loaded AFTER autotest loads, which is too late to get Autotest::Rspec2 to load, so neither will help you in this case.
March 18th, 2010 at 4:39 am
David,
Thanks for your work on this project. I am wondering why you decided to get rid of the autospec command. Your blog post seems to indicate that it was because the command didn’t do much. But sometimes, those are the best kinds of commands.
Personally, I would rather have a command, installed with the Gem, than to have to add an autotest directory to each project. I realize its easy to do, but it just seems more like an environment issue and less of a project issue and so the command seems to fit better.
Unless, of course, the directory and file will be commonly added anyway and have other uses, I am not that familiar with autotest, so I am not sure.
Just my $0.02. Once again, thanks for your work on the project.
March 18th, 2010 at 4:42 am
@Gabriel - I found the spec.opts problem and fixed it in git - fix will be in the beta.5 release, sometime in the next week. In the mean time, just delete spec.opts and all should be well.
March 19th, 2010 at 5:04 pm
After making the file I get this error running autotest:
bill-lazars-macbook-pro-2:wgd1 billlazar$ gem list rspec
*** LOCAL GEMS ***
rspec (2.0.0.beta.4) rspec-core (2.0.0.beta.4, 2.0.0.beta.3) rspec-expectations (2.0.0.beta.4, 2.0.0.beta.3) rspec-mocks (2.0.0.beta.4, 2.0.0.beta.3) rspec-rails (2.0.0.beta.4, 2.0.0.beta.3) bill-lazars-macbook-pro-2:wgd1 billlazar$ autotest loading autotest/rails_rspec2 Error loading Autotest style autotest/rails_rspec2 (no such file to load — autotest/rails_rspec2). Aborting.
March 19th, 2010 at 5:04 pm
Sorry, maybe this will be better formatted (1 try):
bill-lazars-macbook-pro-2:wgd1 billlazar$ gem list rspec Failed to load /Users/billlazar/.gemrc due to permissions problem. Failed to load /Users/billlazar/.gemrc due to permissions problem.
*** LOCAL GEMS ***
rspec (2.0.0.beta.4)
rspec-core (2.0.0.beta.4, 2.0.0.beta.3)
rspec-expectations (2.0.0.beta.4, 2.0.0.beta.3)
rspec-mocks (2.0.0.beta.4, 2.0.0.beta.3)
rspec-rails (2.0.0.beta.4, 2.0.0.beta.3)
bill-lazars-macbook-pro-2:wgd1 billlazar$ autotest
loading autotest/rails_rspec2
Error loading Autotest style autotest/rails_rspec2 (no such file to load — autotest/rails_rspec2). Aborting.
March 21st, 2010 at 2:34 pm
@BillSaysThis - I didn’t add rails support yet. I’ll do that for the next beta release.
March 31st, 2010 at 7:35 pm
Related to the -O option, it seems the error is that rspec 2 uses lowercase args , whereas they where uppercase in rspec 1.x (and autotest seems to expect rspec to have a -O option, intead of the -o). I temporarily got things to work by changing rspec-core-2.0.0.beta.4/lib/rspec/core/command_line_options.rb:42 from ‘-o’ to ‘-O’, but it seems you already found the problem.
I don’t get any errors when running autotest now, but it doesn’t runs anything after I save a file it was supposed to be observing (for example, it doesn’t do anything after i edit a User model generated with scaffold), although it does rerun the entire suite when i send an interrupt signal to the command line.
I am testing it under Ruby 1.9.1, Rails 3.0.0.beta, rspec 2.0.0.beta4, rspec-rails 2.0.0.beta4, ZenTest 4.3.1 on a ubuntu 9.10 machine. Any ideas on what could be going wrong, and where i should try looking further for a solution about this?
March 31st, 2010 at 8:05 pm
Sorry, please do not take into consideration my last comment, as i didn’t had autotest-rails installed, so i hadn’t gotten the same error as Bill did and thought our cases where different. But as soon as i installed it, i got the same error as him and so your answer to him also applies to me.
Anyway, rspec 2 is doing great with very few roadbumps, congratulations!
April 6th, 2010 at 7:03 am
Hey,
is there currently any way to get autotest working with Rails 3 project?
April 7th, 2010 at 4:21 am
@Vojto
still doesn’t seem to work with rspec2 beta5. David said maybe next beta release a few posts earlier.
May 3rd, 2010 at 1:18 am
If after following David’s gist at http://gist.github.com/365816 you type “autotest” and get the following message:
Try navigating to the root of your app, opening autotest/discover.rb, and adding these lines:
Autotest.add_discovery { “rails” } Autotest.add_discovery { “rspec2.0.0.beta.8″ } Autotest.autodiscover # => ["rails", "rspec2"]
Hope this helps!
– Fred
May 3rd, 2010 at 4:09 pm
Hi,
Thanks for all your hard work David. I’ve been doing Rails on and off for about two years now and I figured it was about time I took testing seriously so I bought your RSpec book from Pragmatic Programmers.
I’m just embarking upon a 4 month Rails project and I wanted to create it in Rails 3 as I’m hoping that this initial 4 months will merely be a starting point for a much longer project and i didn’t want the App to be out of date before I’d even started.
I’m having some problems getting RSpec to play nice with my app though.
Firstly, If i navigate to the project root and run $> rspec spec then I get:
/Users/kevinmonk/.rvm/gems/ruby-1.9.2-head@rails300beta/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:233:in
require': no such file to load -- spec/controllers/posts_controller_spec.rb (LoadError) from /Users/kevinmonk/.rvm/gems/ruby-1.9.2-head@rails300beta/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:233:inblock in require_files_to_run’ from /Users/kevinmonk/.rvm/gems/ruby-1.9.2-head@rails300beta/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:233:inmap' from /Users/kevinmonk/.rvm/gems/ruby-1.9.2-head@rails300beta/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:233:inrequire_files_to_run’ from /Users/kevinmonk/.rvm/gems/ruby-1.9.2-head@rails300beta/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/runner.rb:37:inconfigure' from /Users/kevinmonk/.rvm/gems/ruby-1.9.2-head@rails300beta/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/runner.rb:24:inrun’ from /Users/kevinmonk/.rvm/gems/ruby-1.9.2-head@rails300beta/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/runner.rb:12:in `block in autorun’Secondly, the autotest/discover.rb file seems to do nothing. I can type gibberish in this file and run autotest and autotest will start up happily and run my cucumber tests.
May 5th, 2010 at 11:57 am
@Kevin - please report problems like these to http://rubyforge.org/mailman/listinfo/rspec-users - better forum for a conversation.
July 17th, 2010 at 4:38 pm
Hi all,
David, great job with RSpec2!
In case someone is still coming across “Autotest style autotest/rails_rspec2″ issues with running
autotest,bundle exec autotestmay help.I’ve put more details here: http://exviva.posterous.com/running-autotest-with-rails-3.
December 8th, 2010 at 9:31 pm
gasdg