Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Better message if #and_raise exception type requires constructor params #100

Merged
merged 2 commits into from

3 participants

@coreyhaines

Made message so very nice.
#99

@tcrayford

Add at the end of the message " arguments"?

Yeah, yeah. :) Tried to wordsmith it to not need to implement a pluralize method. :) 1 argument, 2 arguments. Ugh!
And, yes, it could be as simple as (["s"]*(arity-1)).uniq.to_s (confusing enough?) HAHA!

@coreyhaines

Looks like it committed a bunch of "get rid of ending space" changes, too. :)

@dchelimsky dchelimsky merged commit 7ce8c6f into rspec:master
@dchelimsky dchelimsky referenced this pull request from a commit
@dchelimsky dchelimsky More detailed error message when and_raise gets a class whose constru…
…ctor requires 1 or more args.

- #99, #100.
1f28dcd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 lib/rspec/mocks/message_expectation.rb
@@ -178,7 +178,11 @@ def invoke(*args, &block)
@order_group.handle_order_constraint self
begin
- Kernel::raise(@exception_to_raise) unless @exception_to_raise.nil?
+ begin
+ Kernel::raise(@exception_to_raise) unless @exception_to_raise.nil?
+ rescue ArgumentError => ex
+ Kernel::raise ex.exception("Errors raised by expectations cannot have constructors that take arguments. #{@exception_to_raise.to_s} has a constructor requiring #{@exception_to_raise.instance_method(:initialize).arity}.")
+ end
Kernel::throw(*@args_to_throw) unless @args_to_throw.empty?
default_return_val = if !@method_block.nil?
View
30 spec/rspec/mocks/mock_spec.rb
@@ -8,7 +8,7 @@ module Mocks
end
treats_method_missing_as_private :subject => RSpec::Mocks::Mock.new, :noop => false
-
+
after(:each) do
@mock.rspec_reset
end
@@ -54,7 +54,7 @@ module Mocks
@mock.not_expected
violated
}.to raise_error(
- RSpec::Mocks::MockExpectationError,
+ RSpec::Mocks::MockExpectationError,
%Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
)
end
@@ -65,7 +65,7 @@ module Mocks
@mock.not_expected("unexpected text")
violated
}.to raise_error(
- RSpec::Mocks::MockExpectationError,
+ RSpec::Mocks::MockExpectationError,
%Q|(Double "test double").not_expected("unexpected text")\n expected: 0 times\n received: 1 time|
)
end
@@ -137,19 +137,19 @@ module Mocks
@mock.rspec_verify
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
end
-
+
describe 'with a method that has a default argument' do
it "raises an exception if the arguments don't match when the method is called, correctly reporting the offending arguments" do
def @mock.method_with_default_argument(arg={}); end
@mock.should_receive(:method_with_default_argument).with({})
-
+
expect {
@mock.method_with_default_argument(nil)
@mock.rspec_verify
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :method_with_default_argument with unexpected arguments\n expected: ({})\n got: (nil)")
end
end
-
+
it "fails if unexpected method called" do
lambda {
@mock.something("a","b","c")
@@ -188,7 +188,7 @@ def @mock.method_with_default_argument(arg={}); end
it "fails right away when method defined as never is received" do
@mock.should_receive(:not_expected).never
expect { @mock.not_expected }.to raise_error(
- RSpec::Mocks::MockExpectationError,
+ RSpec::Mocks::MockExpectationError,
%Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
)
end
@@ -196,7 +196,7 @@ def @mock.method_with_default_argument(arg={}); end
it "eventually fails when method defined as never is received" do
@mock.should_receive(:not_expected).never
expect { @mock.not_expected }.to raise_error(
- RSpec::Mocks::MockExpectationError,
+ RSpec::Mocks::MockExpectationError,
%Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
)
end
@@ -216,6 +216,18 @@ def @mock.method_with_default_argument(arg={}); end
}.should raise_error(RuntimeError, "error message")
end
+ it "fails with nice message if passed Exception requires constructor arguments" do
+ class ErrorThatHasAConstructorTakingArguments < RuntimeError
+ def initialize(i_take_an_argument)
+ end
+ end
+
+ @mock.should_receive(:something).and_raise(ErrorThatHasAConstructorTakingArguments)
+ lambda {
+ @mock.something
+ }.should raise_error(ArgumentError, "Errors raised by expectations cannot have constructors that take arguments. RSpec::Mocks::ErrorThatHasAConstructorTakingArguments has a constructor requiring 1.")
+ end
+
it "raises RuntimeError with passed message" do
@mock.should_receive(:something).and_raise("error message")
lambda {
@@ -615,7 +627,7 @@ def add_call
mock = double()
expect {mock.foo}.to raise_error(/Double received/)
end
-
+
it "does respond to initially stubbed methods" do
double = double(:foo => "woo", :bar => "car")
double.foo.should eq "woo"
Something went wrong with that request. Please try again.