Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/rspec/mocks/message_expectation.rb
Expand Up @@ -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?
Expand Down
30 changes: 21 additions & 9 deletions spec/rspec/mocks/mock_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -188,15 +188,15 @@ 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

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
Expand All @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down