Controlling your rake spec run

Run a single file in rake spec

Sometimes, you want to be able to run a single spec file from rake spec instead of rspec because there is additional environment setup in the rake task that changes the outcome of the test run (maybe ensuring a dependency is built?)

If you run rake spec with the SPEC environment variable set to the path of the spec file you wish to run, you can run that spec in isolation in the context of the rake setup:

rake spec SPEC=path/to/file/from/project/root_spec.rb

You can even go so far as to include a line number as you do for rspec runs:

rake spec SPEC=path/to/file/from/project/root_spec.rb:42

The above will run only the specs that have that line in context (recall that if outside of an example, specifying a line number will include all of the scope that that line number is a part of.

Change formatting

Another rake spec option you might want to change is changing the formatting to documentation to be more descriptive so that you can see the failed specs described as they happen instead of waiting for the summary:

rake spec SPEC_OPTS="--format documentation"

SPEC_OPTS is useful for any scenario in which you want to pass an option normally passed to the rspec command.

Coverage options

I’ve never really wanted to change code coverage options on the fly, but you can specify RCOV_OPTIONS="--option-name value"

See also APIDock Spec::Rake::SpecTask

Leave a Reply

%d bloggers like this: