diff options
author | Benoit Daloze <eregontp@gmail.com> | 2021-03-27 13:02:38 +0100 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2021-03-27 13:02:38 +0100 |
commit | 44736a6b7a2b3475db2d05187f33e3c1a7b4b4e5 (patch) | |
tree | 4cb679a8742121782a50e72e01160d19f479f9a6 /spec/mspec/spec/matchers/raise_error_spec.rb | |
parent | 31ae931e166825450dcc16d470553e67281951a2 (diff) |
Update to ruby/mspec@d1adf59
Diffstat (limited to 'spec/mspec/spec/matchers/raise_error_spec.rb')
-rw-r--r-- | spec/mspec/spec/matchers/raise_error_spec.rb | 90 |
1 files changed, 57 insertions, 33 deletions
diff --git a/spec/mspec/spec/matchers/raise_error_spec.rb b/spec/mspec/spec/matchers/raise_error_spec.rb index a40acc0ea0..8613eee118 100644 --- a/spec/mspec/spec/matchers/raise_error_spec.rb +++ b/spec/mspec/spec/matchers/raise_error_spec.rb @@ -1,79 +1,94 @@ require 'spec_helper' -require 'mspec/expectations/expectations' -require 'mspec/matchers' class ExpectedException < Exception; end class UnexpectedException < Exception; end -describe RaiseErrorMatcher do +RSpec.describe RaiseErrorMatcher do + before :each do + state = double("run state").as_null_object + allow(MSpec).to receive(:current).and_return(state) + end + it "matches when the proc raises the expected exception" do proc = Proc.new { raise ExpectedException } matcher = RaiseErrorMatcher.new(ExpectedException, nil) - matcher.matches?(proc).should == true + expect(matcher.matches?(proc)).to eq(true) end - it "executes its optional block if matched" do + it "executes its optional {/} block if matched" do + ensure_mspec_method(-> {}.method(:should)) + run = false - proc = Proc.new { raise ExpectedException } - matcher = RaiseErrorMatcher.new(ExpectedException, nil) { |error| + -> { raise ExpectedException }.should PublicMSpecMatchers.raise_error { |error| + expect(error.class).to eq(ExpectedException) run = true - error.class.should == ExpectedException } + expect(run).to eq(true) + end - matcher.matches?(proc).should == true - run.should == true + it "executes its optional do/end block if matched" do + ensure_mspec_method(-> {}.method(:should)) + + run = false + -> { raise ExpectedException }.should PublicMSpecMatchers.raise_error do |error| + expect(error.class).to eq(ExpectedException) + run = true + end + expect(run).to eq(true) end it "matches when the proc raises the expected exception with the expected message" do proc = Proc.new { raise ExpectedException, "message" } matcher = RaiseErrorMatcher.new(ExpectedException, "message") - matcher.matches?(proc).should == true + expect(matcher.matches?(proc)).to eq(true) end it "matches when the proc raises the expected exception with a matching message" do proc = Proc.new { raise ExpectedException, "some message" } matcher = RaiseErrorMatcher.new(ExpectedException, /some/) - matcher.matches?(proc).should == true + expect(matcher.matches?(proc)).to eq(true) end it "does not match when the proc does not raise the expected exception" do exc = UnexpectedException.new matcher = RaiseErrorMatcher.new(ExpectedException, nil) - matcher.matching_exception?(exc).should == false - lambda { + expect(matcher.matching_exception?(exc)).to eq(false) + expect { matcher.matches?(Proc.new { raise exc }) - }.should raise_error(UnexpectedException) + }.to raise_error(UnexpectedException) end it "does not match when the proc raises the expected exception with an unexpected message" do exc = ExpectedException.new("unexpected") matcher = RaiseErrorMatcher.new(ExpectedException, "expected") - matcher.matching_exception?(exc).should == false - lambda { + expect(matcher.matching_exception?(exc)).to eq(false) + expect { matcher.matches?(Proc.new { raise exc }) - }.should raise_error(ExpectedException) + }.to raise_error(ExpectedException) end it "does not match when the proc does not raise an exception" do proc = Proc.new {} matcher = RaiseErrorMatcher.new(ExpectedException, "expected") - matcher.matches?(proc).should == false + expect(matcher.matches?(proc)).to eq(false) end it "provides a useful failure message when the exception class differs" do exc = UnexpectedException.new("message") matcher = RaiseErrorMatcher.new(ExpectedException, "message") - matcher.matching_exception?(exc).should == false + expect(matcher.matching_exception?(exc)).to eq(false) begin matcher.matches?(Proc.new { raise exc }) rescue UnexpectedException => e - matcher.failure_message.should == + expect(matcher.failure_message).to eq( ["Expected ExpectedException (message)", "but got: UnexpectedException (message)"] - ExceptionState.new(nil, nil, e).message.should == + ) + expect(ExceptionState.new(nil, nil, e).message).to eq( "Expected ExpectedException (message)\nbut got: UnexpectedException (message)" + ) else raise "no exception" end @@ -83,14 +98,16 @@ describe RaiseErrorMatcher do exc = ExpectedException.new("unexpected") matcher = RaiseErrorMatcher.new(ExpectedException, "expected") - matcher.matching_exception?(exc).should == false + expect(matcher.matching_exception?(exc)).to eq(false) begin matcher.matches?(Proc.new { raise exc }) rescue ExpectedException => e - matcher.failure_message.should == + expect(matcher.failure_message).to eq( ["Expected ExpectedException (expected)", "but got: ExpectedException (unexpected)"] - ExceptionState.new(nil, nil, e).message.should == + ) + expect(ExceptionState.new(nil, nil, e).message).to eq( "Expected ExpectedException (expected)\nbut got: ExpectedException (unexpected)" + ) else raise "no exception" end @@ -100,14 +117,16 @@ describe RaiseErrorMatcher do exc = UnexpectedException.new("unexpected") matcher = RaiseErrorMatcher.new(ExpectedException, "expected") - matcher.matching_exception?(exc).should == false + expect(matcher.matching_exception?(exc)).to eq(false) begin matcher.matches?(Proc.new { raise exc }) rescue UnexpectedException => e - matcher.failure_message.should == + expect(matcher.failure_message).to eq( ["Expected ExpectedException (expected)", "but got: UnexpectedException (unexpected)"] - ExceptionState.new(nil, nil, e).message.should == + ) + expect(ExceptionState.new(nil, nil, e).message).to eq( "Expected ExpectedException (expected)\nbut got: UnexpectedException (unexpected)" + ) else raise "no exception" end @@ -117,16 +136,18 @@ describe RaiseErrorMatcher do proc = Proc.new { 120 } matcher = RaiseErrorMatcher.new(ExpectedException, "expected") matcher.matches?(proc) - matcher.failure_message.should == + expect(matcher.failure_message).to eq( ["Expected ExpectedException (expected)", "but no exception was raised (120 was returned)"] + ) end it "provides a useful failure message when no exception is raised and nil is returned" do proc = Proc.new { nil } matcher = RaiseErrorMatcher.new(ExpectedException, "expected") matcher.matches?(proc) - matcher.failure_message.should == + expect(matcher.failure_message).to eq( ["Expected ExpectedException (expected)", "but no exception was raised (nil was returned)"] + ) end it "provides a useful failure message when no exception is raised and the result raises in #pretty_inspect" do @@ -137,23 +158,26 @@ describe RaiseErrorMatcher do proc = Proc.new { result } matcher = RaiseErrorMatcher.new(ExpectedException, "expected") matcher.matches?(proc) - matcher.failure_message.should == + expect(matcher.failure_message).to eq( ["Expected ExpectedException (expected)", "but no exception was raised (#<Object>(#pretty_inspect raised #<ArgumentError: bad>) was returned)"] + ) end it "provides a useful negative failure message" do proc = Proc.new { raise ExpectedException, "expected" } matcher = RaiseErrorMatcher.new(ExpectedException, "expected") matcher.matches?(proc) - matcher.negative_failure_message.should == + expect(matcher.negative_failure_message).to eq( ["Expected to not get ExpectedException (expected)", ""] + ) end it "provides a useful negative failure message for strict subclasses of the matched exception class" do proc = Proc.new { raise UnexpectedException, "unexpected" } matcher = RaiseErrorMatcher.new(Exception, nil) matcher.matches?(proc) - matcher.negative_failure_message.should == + expect(matcher.negative_failure_message).to eq( ["Expected to not get Exception", "but got: UnexpectedException (unexpected)"] + ) end end |