Skip to content

Commit

Permalink
Support optional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Pohorecki committed May 20, 2013
1 parent 0cc7db8 commit ea0a8db
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/bogus/interaction.rb
Expand Up @@ -18,6 +18,10 @@ def any_args?
args == [AnyArgs]
end

def args
super.reject { |arg| arg.eql?(Bogus::DefaultValue) }
end

private

def same_args?(other)
Expand Down
2 changes: 2 additions & 0 deletions spec/bogus/interaction_spec.rb
Expand Up @@ -5,6 +5,7 @@ class SomeError < StandardError; end

same = [
[[:foo, [:bar], "value"], [:foo, [:bar], "value"]],
[[:foo, [:bar, Bogus::DefaultValue], "value"], [:foo, [:bar], "value"]],
[[:foo, [:bar]], [:foo, [:bar], "value"]],
[[:foo, [:bar], "value"], [:foo, [:bar]]],
[[:foo, [:bar]], [:foo, [:bar]]],
Expand All @@ -21,6 +22,7 @@ class SomeError < StandardError; end
[[:foo, [:bar], "value"], [:foo, [:bar], "value2"]],
[[:foo, [:bar], "value"], [:baz, [:bar], "value"]],
[[:foo, [:baz], "value"], [:foo, [:bar], "value"]],
[[:foo, [Bogus::DefaultValue, :baz], "value"], [:foo, [:bar, :bar], "value"]],
[[:foo, [Bogus::AnyArgs]], [:bar, [:bar]]],
[[:foo, [:bar]], [:bar, [Bogus::AnyArgs]]],
[[:foo, [Bogus::AnyArgs], "some value"], [:foo, [:bar], "other value"]],
Expand Down
18 changes: 18 additions & 0 deletions spec/bogus/mocking_dsl_spec.rb
Expand Up @@ -8,6 +8,9 @@ def foo(bar)
def hello(greeting, name)
end

def with_optional_args(x, y = 1)
end

def self.bar(baz)
"Hello #{baz}"
end
Expand Down Expand Up @@ -85,6 +88,12 @@ class Stubber
the_fake.should Stubber.have_received.foo("test", "test 2")
}.to raise_error(ArgumentError)
end

it "allows spying on methods with optional parameters" do
the_fake.with_optional_args(123)

the_fake.should Stubber.have_received.with_optional_args(123)
end
end

it "can be used with plain old Ruby objects" do
Expand All @@ -96,6 +105,15 @@ class Stubber
object.should Stubber.have_received.foo("test")
end

it "allows spying on methods with optional parameters" do
object = ExampleFoo.new
Stubber.stub(object).with_optional_args(123) { 999 }

object.with_optional_args(123).should == 999

object.should Stubber.have_received.with_optional_args(123)
end

class ClassWithMatches
def matches?(something)
end
Expand Down

0 comments on commit ea0a8db

Please sign in to comment.