Search code examples
ruby-on-railsrubyrspecrspec-railsnewrelic

Raise exception if RSpec fails


I'd like to write rake script, that runs all RSpec tests in my application. If any of the tests fails, id like to throw an Exception in the task (later on I will catch this exception in NewRelic alert system - I use it for other tasks as well).

Is it possible?


Solution

  • First, you don't need to raise an exception to let newrelic know of it. You can let newrelic know by directly posting error details to their api: https://docs.newrelic.com/docs/agents/ruby-agent/troubleshooting/sending-new-relic-handled-errors

    notice_error(exception, options = { })
    

    where exception can be an exception object (StandardError.new, for example) or a message.

    Also, you can omit all this exception business and check exit code of rspec command line tool. If tests are green, it'll be zero. If errors are present, it will not be zero. Something like this

    if system('rspec spec') # return true if command was successful, false otherwise
      # if green
    else
      # if red
    end