Skip to content

Commit

Permalink
Add #new safe stubbing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Terrell committed Jan 14, 2015
1 parent 43fa002 commit 2cd4013
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/bogus/stubbing/verifies_stub_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def verify!(object, method_name, args)
stubbing_non_existent_method!(object, method_name) unless object.respond_to?(method_name)
return unless object.methods.include?(method_name)
return if WithArguments.with_matcher?(args)
method = object.method(method_name)
if method_name.to_sym == :new && object.kind_of?(Class)
method = object.allocate.method(:initialize)
else
method = object.method(method_name)
end
verify_call!(method, args)
end

Expand Down
15 changes: 14 additions & 1 deletion spec/bogus/stubbing/verifies_stub_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

describe Bogus::VerifiesStubDefinition do
class ExampleForVerify
def initialize(test)

end
def foo(bar)
end

Expand All @@ -15,7 +18,7 @@ def var_args(x, *y)
end
end

let(:object) { ExampleForVerify.new }
let(:object) { ExampleForVerify.new(1) }
let(:verifies_stub_definition) { Bogus::VerifiesStubDefinition.new(method_stringifier) }
let(:method_stringifier) { isolate(Bogus::MethodStringifier) }

Expand Down Expand Up @@ -47,6 +50,16 @@ def self.it_disallows_argument_numbers(method_name, *arg_counts)
end
end

context "when checking #new" do
let(:object) { ExampleForVerify }
it "checks arity" do
it_allows(:new, [1])
end
it "errors with the wrong number of arguments" do
it_disallows(:new, [])
end
end

it "checks for method presence" do
it_disallows(:bar, [1], NameError)
end
Expand Down

0 comments on commit 2cd4013

Please sign in to comment.