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.

24 Responses to “rspec-2 and autotest”

  1. Nick Says:

    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.

  2. David Chelimsky Says:

    We’re updating the book for rspec-2 and rails-3 before we go to print.

  3. Daniel DeLeo Says:

    Hi David, Will that magical incantation work if I put it in my ~/.autotest file?

  4. Gabriel Sobrinho Says:

    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:in run' 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’

  5. David Chelimsky Says:

    @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.

  6. David Chelimsky Says:

    @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.

  7. Gabriel Sobrinho Says:

    @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?

  8. Gabriel Sobrinho Says:

    I deleted my spec.opts file and this works. Strange, I will look this.

  9. BillSaysThis Says:

    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!

  10. David Chelimsky Says:

    @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.

  11. Randy Syring Says:

    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.

  12. David Chelimsky Says:

    @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.

  13. BillSaysThis Says:

    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.

  14. BillSaysThis Says:

    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.

  15. David Chelimsky Says:

    @BillSaysThis - I didn’t add rails support yet. I’ll do that for the next beta release.

  16. Stefano Diem Benatti Says:

    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?

  17. Stefano Diem Benatti Says:

    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!

  18. Vojto Says:

    Hey,

    is there currently any way to get autotest working with Rails 3 project?

  19. mooman Says:

    @Vojto

    still doesn’t seem to work with rspec2 beta5. David said maybe next beta release a few posts earlier.

  20. Fred Schoeneman Says:

    If after following David’s gist at http://gist.github.com/365816 you type “autotest” and get the following message:

    Autotest style autotest/rails_rspec2.0.0.beta.8 doesn’t >seem to exist. Aborting.

    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

  21. Kevin Monk Says:

    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:in map' 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:in configure' 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.

  22. David Chelimsky Says:

    @Kevin - please report problems like these to http://rubyforge.org/mailman/listinfo/rspec-users - better forum for a conversation.

  23. Olek Says:

    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 autotest may help.

    I’ve put more details here: http://exviva.posterous.com/running-autotest-with-rails-3.

  24. Michelle Pace Says:

    gasdg

Leave a Reply