Skip to content

Instantly share code, notes, and snippets.

@erwanlr
Created July 3, 2015 11:21
Show Gist options
  • Select an option

  • Save erwanlr/60c3d7ceee600e88ba96 to your computer and use it in GitHub Desktop.

Select an option

Save erwanlr/60c3d7ceee600e88ba96 to your computer and use it in GitHub Desktop.

Revisions

  1. erwanlr created this gist Jul 3, 2015.
    35 changes: 35 additions & 0 deletions errors_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    require 'rspec'
    require 'optparse'

    module Test
    class Error < StandardError
    end

    class AnotherError < Error
    def to_s
    'this message exactly'
    end
    end
    end

    RSpec.describe 'matching error message with string' do
    it 'matches the error message' do
    expect { raise StandardError, 'this message exactly'}.
    to raise_error(StandardError, 'this message exactly')
    end

    it 'matches the error message' do
    expect { raise Test::Error, 'this message exactly' }.
    to raise_error(Test::Error, 'this message exactly')
    end

    it 'matches the error message' do
    expect { raise Test::AnotherError }.
    to raise_error(Test::AnotherError, 'this message exactly')
    end

    it 'matches the error message, but does not :x' do
    expect { raise OptionParser::InvalidArgument, 'this message exactly'}.
    to raise_error(OptionParser::InvalidArgument, 'this message exactly')
    end
    end