Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Throwing with parameters #90

Merged
merged 2 commits into from

3 participants

@pat
pat commented

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. Let me know if it needs changing.

@pat pat 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.
aca77b2
@dchelimsky
Owner

It is simple. Maybe too simple, as the method signature and the docs aren't clear about the number of args. AFAIK, a throw can only take one additional argument, not varargs. How about making it throw_symbol(symbol, value=nil)? That aligns better with object.should throw(symbol, value) in rspec-expectations.

We should also do the same for and_raise (i.e. align it with rspec-expectations). I'll look into that as well once this gets merged. Unless of course you want to :)

@pat pat Following Kernel.throw's method signature
First argument is the symbol, second is the optional object that is returned.
d0c805e
@pat
pat commented

I went with object instead of value, just because that's what the Pickaxe has (well, it has obj, but I'll be damned if I'm going to abbreviate that). The object value defaults to nil, so I've not added a second instance variable and am just storing them as an array much like my first implementation.

I'll leave and_raise with you, though :)

@dchelimsky dchelimsky merged commit c03c8d6 into rspec:master
@dchelimsky dchelimsky referenced this pull request from a commit
@dchelimsky dchelimsky fix warnings
- #90
4543082
@myronmarston myronmarston commented on the diff
lib/rspec/mocks/message_expectation.rb
@@ -130,8 +130,8 @@ def and_raise(exception=Exception)
# @example
#
# car.stub(:go).and_throw(:out_of_gas)
- def and_throw(symbol)
- @symbol_to_throw = symbol
+ def and_throw(symbol, object = nil)
+ @symbol_to_throw = [symbol, object]
@myronmarston Owner

FWIW, I think it's confusing to have an instance variable named @symbol_to_throw actually be an array, not a symbol.

Maybe @throw_args would be a more accurate name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@myronmarston
Owner

Ha..I missed that on it. Nice to see you're already on it :).

@dchelimsky
Owner

Nice to see you would have been had I not been :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 6, 2011
  1. @pat

    Throwing with parameters

    pat authored
    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.
  2. @pat

    Following Kernel.throw's method signature

    pat authored
    First argument is the symbol, second is the optional object that is returned.
This page is out of date. Refresh to see the latest.
View
6 lib/rspec/mocks/message_expectation.rb
@@ -130,8 +130,8 @@ def and_raise(exception=Exception)
# @example
#
# car.stub(:go).and_throw(:out_of_gas)
- def and_throw(symbol)
- @symbol_to_throw = symbol
+ def and_throw(symbol, object = nil)
+ @symbol_to_throw = [symbol, object]
@myronmarston Owner

FWIW, I think it's confusing to have an instance variable named @symbol_to_throw actually be an array, not a symbol.

Maybe @throw_args would be a more accurate name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
# Tells the object to yield one or more args to a block when the message
@@ -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)
View
18 spec/rspec/mocks/stub_spec.rb
@@ -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
Something went wrong with that request. Please try again.