Every repository with this icon (
Every repository with this icon (
Custom Formatters
RSpec generates the output you see on the command line using formatters that receive messages on events like example_group_started, example_passed, and example_failed.
One question that came up recently was how to get rspec to abort on the first failure. A custom formatter is a simple solution for this. We can start with the progress bar formatter and override one method:
require 'spec/runner/formatter/progress_bar_formatter'
class AbortOnFirstFailureFormatter < Spec::Runner::Formatter::ProgressBarFormatter
def example_failed(example, counter, failure)
super
@output.puts "Aborting after first failure"
exit
end
end
To use this formatter, you need to require the file it lives in and pass the classname to the format argument on the command line:
spec spec --require spec/support/formatters/abort_on_first_failure_formatter.rb --format AbortOnFirstFailureFormatter
As that is a fair amount to type, you could also set those arguments up in a rake task, like this:
namespace :spec do
Spec::Rake::SpecTask.new('abort_early') do |t|
t.spec_opts = %w[--require spec/support/formatters/abort_on_first_failure_formatter.rb --format AbortOnFirstFailureFormatter]
end
end
Now just type ‘rake spec:abort_early’ and you’re off and running.
Take a look at the rdoc for Spec::Runner::Formatter::BaseFormatter for more information.






