From ed48b80d58085befb0491f960b72528322eb0c16 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 17 Jan 2020 21:58:29 -0700 Subject: [PATCH] Add RSpec `raise_error` matcher spec --- .../expectations/raise_error_matcher_spec.cr | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 spec/rspec/expectations/raise_error_matcher_spec.cr diff --git a/spec/rspec/expectations/raise_error_matcher_spec.cr b/spec/rspec/expectations/raise_error_matcher_spec.cr new file mode 100644 index 0000000..85678a7 --- /dev/null +++ b/spec/rspec/expectations/raise_error_matcher_spec.cr @@ -0,0 +1,94 @@ +require "../../spec_helper" + +# Examples taken from: +# https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/built-in-matchers/raise-error-matcher +# and modified to fit Spectator and Crystal. +Spectator.describe "`raise_error` matcher" do + context "expect any error" do + # This example originally calls a non-existent method. + # That isn't allowed in Crystal. + # The example has been changed to just raise a runtime error. + describe "dividing by zero" do + it "raises" do + expect { 42 // 0 }.to raise_error + end + end + end + + context "expect specific error" do + # Again, can't even compile if a method doesn't exist. + # So using a different exception here. + describe "dividing by zero" do + it "raises" do + expect { 42 // 0 }.to raise_error(DivisionByZeroError) + end + end + end + + # The following examples are changed slightly. + # `raise Type.new(message)` is the syntax in Crystal, + # whereas it is `raise Type, message` in Ruby. + # Additionally, `StandardError` doesn't exist in Crystal, + # so `Exception` is used instead. + context "match message with a string" do + describe "matching error message with string" do + it "matches the error message" do + expect { raise Exception.new("this message exactly") } + .to raise_error("this message exactly") + end + end + end + + context "match message with a regexp" do + describe "matching error message with regex" do + it "matches the error message" do + expect { raise Exception.new("my message") } + .to raise_error(/my mess/) + end + end + end + + context "matching message with `with_message`" do + describe "matching error message with regex" do + it "matches the error message" do + expect { raise Exception.new("my message") } + .to raise_error.with_message(/my mess/) + end + end + end + + context "match class + message with string" do + describe "matching error message with string" do + it "matches the error message" do + expect { raise Exception.new("this message exactly") } + .to raise_error(Exception, "this message exactly") + end + end + end + + context "match class + message with regexp" do + describe "matching error message with regex" do + it "matches the error message" do + expect { raise Exception.new("my message") } + .to raise_error(Exception, /my mess/) + end + end + end + + # TODO: Support passing a block to `raise_error` matcher. + # context "set expectations on error object passed to block" do + # it "raises DivisionByZeroError" do + # expect { 42 // 0 }.to raise_error do |error| + # expect(error).to be_a(DivisionByZeroError) + # end + # end + # end + + context "expect no error at all" do + describe "#to_s" do + it "does not raise" do + expect { 42.to_s }.not_to raise_error + end + end + end +end