Permalink
Browse files

Throwing with parameters

This is written so I can stub out Warden's behaviour appropriately - and I realise it's simple, but perhaps this is all that's needed. It certainly works for me.
  • Loading branch information...
1 parent 26d3ffb commit aca77b2a330213b9feeb689cb29d69a8833ede50 @pat pat committed Dec 6, 2011
Showing with 14 additions and 8 deletions.
  1. +2 −2 lib/rspec/mocks/message_expectation.rb
  2. +12 −6 spec/rspec/mocks/stub_spec.rb
@@ -130,7 +130,7 @@ def and_raise(exception=Exception)
# @example
#
# car.stub(:go).and_throw(:out_of_gas)
- def and_throw(symbol)
+ def and_throw(*symbol)
@symbol_to_throw = symbol
end
@@ -173,7 +173,7 @@ def invoke(*args, &block)
begin
Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
- Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
+ Kernel::throw *@symbol_to_throw unless @symbol_to_throw.nil?
default_return_val = if !@method_block.nil?
invoke_method_block(*args, &block)
@@ -28,7 +28,7 @@ def existing_private_instance_method
@instance = @class.new
@stub = Object.new
end
-
+
[:stub!, :stub].each do |method|
describe "using #{method}" do
it "returns declared value when message is received" do
@@ -146,7 +146,7 @@ def existing_private_instance_method
current_value.should eq [:yielded_value, :another_value]
@instance.rspec_verify
end
-
+
it "yields a specified object and return another specified object" do
yielded_obj = double("my mock")
yielded_obj.should_receive(:foo).with(:bar)
@@ -160,7 +160,13 @@ def existing_private_instance_method
@stub.something
end.should throw_symbol(:up)
end
-
+
+ it "throws with given arguments when told to" do
+ @stub.stub(:something).and_throw(:up, :over => :there)
+ result = catch(:up) { @stub.something }
+ result.should == {:over => :there}
+ end
+
it "overrides a pre-existing method" do
@stub.stub(:existing_instance_method).and_return(:updated_stub_value)
@stub.existing_instance_method.should eq :updated_stub_value
@@ -171,7 +177,7 @@ def existing_private_instance_method
@stub.stub(:foo) { 'baz' }
@stub.foo.should eq 'baz'
end
-
+
it "allows a stub and an expectation" do
@stub.stub(:foo).with("bar")
@stub.should_receive(:foo).with("baz")
@@ -185,7 +191,7 @@ def existing_private_instance_method
@stub.rspec_verify
end
end
-
+
describe "A method stub with args" do
before(:each) do
@stub = Object.new
@@ -225,7 +231,7 @@ def existing_private_instance_method
@stub.foo("other")
end.should raise_error
end
-
+
it "supports options" do
@stub.stub(:foo, :expected_from => "bar")
end

0 comments on commit aca77b2

Please sign in to comment.