Rspec and the Rspec-Rails plugin are awesome. Made even more awesome by using Autotest. Which is made even more awesome by using growl-glue :)
We’ve noticed on a couple of our projects that Autotest will not run all of our specs that would normally be run by ”% rake spec”. Looking into it further, I saw that the rspec-rails plugin sets up a number of Autotest “mappings”, which are ways of telling the Autotest loop of not only the files that should be tested, but also which specs to run when a source file is modified.
The default mappings can be found in:
RAILS_ROOT/vendor/plugins/rspec-rails/lib/autotest
Autotest#add_mapping takes a regular expression that, if it matches a source file path, can either then return a path or a list of paths that point to the specs that correspond to the source file. If you wanted to return a glob of files, you can use Autotest#files_matching, which takes a regular expression that will return a set of specs that match.
In my case, there are some files in “app/filters” that I want to link up to some specs in “spec/filters”. So, in my .autotest file inside of the project root, I include the following:
Autotest.add_hook :initialize do |autotest|
autotest.add_mapping(%r%^app/filters/(.*)\.rb$%) { |_, m|
autotest.files_matching %r%^spec/filters/.*_spec.rb$%
}
end
Here I’m pretty aggressive – I simply run all of the filter specs in spec/filters when any file in app/filters is modified. You can get pretty specific though – make sure to check out the default autotest configuration inside of the rspec-rails plugin to get an idea for what’s possible.
If you’re unsure about which specs are not getting run, try running autotest as:
% autotest -v
Autotest will say “Dunno!” for each file for which it doesn’t have a mapping.
