In this video from GoGaRuCo 2011, Corey Haines shows some techniques for making Rails test suites much faster. I would summarize it as follows:
- Put as much of your code as possible outside the Rails app, into other modules and classes
- Test those separately, without the overhead of loading up Rails
- Use them from within your Rails app
There were a couple of things I didn’t understand, though.
- He alternates between running tests with
rspecandspnorspna(for example, at about 3:50). Isspna commonly-known tool? - In his tests for non-Rails classes and modules, he includes the module or class being tested, but I don’t see him including anything like
spec_helper. How does he have Rspec available?
Sorry about the confusion.
spnandspnaare aliases I have that add my non-rails code to rspec’s load path. There isn’t anything special about them, other than adding a-I path_to_codeon the command-line.These days, I add something like this to my
.rspecfile:Then I can do simple
require 'object_name'at the top of my specs.As for not including
spec_helper: that is true, I don’t. When you execute your spec file withrspec <path_to_spec_file>, it gets interpreted, so you don’t need to requirerspecexplicitly.For my db specs these days, I also have built an
active_record_spec_helperwhich requires active_record, establishes a connection to the test database, and sets updatabase_cleaner; this allows me to simply require my model at the top of my spec file. This way, I can test the AR code against the db without having to load up my whole app.A client I am working at where we are using these techniques is interested in supporting some blog posts about this, so hopefully they will start coming out towards the middle of June.