Thursday, March 31, 2011

How do I get autotest to properly inflect singular models and plural controllers

Just as an example, if I have a Book model and a BooksController, autotest, part of the ZenTest suite will pick up the association between the two and load test/unit/book_test.rb and test/functional/books_controller_test.rb into the test suite. On the other hand, if I have a Story model and a StoriesController, autotest refuse to "notice" the test/functional/stories_controller_test.rb

From stackoverflow
  • Unfortunately ZenTest isn't a rails plugin, so it doesn't benefit from ActiveSupport's pluralize method. Therefore it uses simple regular expressions to match the filenames. Have a look at ZenTest/autotest/rails.rb to see a list of the existing mappings for Rails.

    At the end you have two options:

    • (monkey) patch your own mapping rule
    • forget about pluralization

    Hope this helps!

  • You can override the mappings in your .autotest file. Either in your home directory or at the root of the project. You could require 'active_support' there to get String#pluralize and String#singularize.

    Borrow the code from the rspec-rails plugin in lib/autotest/rails_rspec.rb, it already seems to do the singular/plural magic with ActiveSupport. You'll probably need to yank out the RSpec specific assumptions from there, though.

  • I finally figured out what was going on, and it had nothing to do with pluralization after all.

    It had everything to do with the word "stories" which can be a special directory for one of the testing libraries (RSpec? Cucumber? I forget) So it was listed in my ~/.autotest config file as an exception! I'm not sure when I cut and pasted the snippet into the file, probably when I was first getting starting with ZenTest and didn't know what I was really doing.

    Autotest.add_hook :initialize do |at|
      %w{... stories ...}.each {|exception|at.add_exception(exception)}
    end
    

    Adding a trailing slash ("stories/") restored the test and removed the brick marks from my forehead.

    So I guess the lesson learned is: check for stray configuration files when debugging.

0 comments:

Post a Comment