Skip to content

Commit

Permalink
Validate invocation args for verified null object doubles.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Nov 25, 2014
1 parent efda49c commit 04e048f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Bug Fixes:
work properly when `SomeConst` has previously been stubbed.
`(instance|class)_double("SomeClass")` already worked properly.
(Myron Marston, #824)
* Validate invocation args for null object verified doubles.
(Myron Marston, #829)

### 3.1.3 / 2014-10-08
[Full Changelog](http://github.com/rspec/rspec-mocks/compare/v3.1.2...v3.1.3)
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/mocks/verifying_double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def method_missing(message, *args, &block)
else
__mock_proxy.ensure_publicly_implemented(message, self)
end

__mock_proxy.validate_arguments!(message, args)
end

super
Expand Down
6 changes: 4 additions & 2 deletions lib/rspec/mocks/verifying_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def method_reference
def visibility_for(method_name)
method_reference[method_name].visibility
end

def validate_arguments!(method_name, args)
@method_doubles[method_name].validate_arguments!(args)
end
end

# @private
Expand Down Expand Up @@ -131,8 +135,6 @@ def proxy_method_invoked(obj, *args, &block)
super
end

private

def validate_arguments!(actual_args)
@method_reference.with_signature do |signature|
verifier = Support::StrictSignatureVerifier.new(signature, actual_args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ module Mocks
prevents { o.undefined_method }
end

it 'verifies arguments for null objects' do
o = class_double('LoadedClass').as_null_object

expect {
o.defined_class_method(:too, :many, :args)
}.to raise_error(ArgumentError, "Wrong number of arguments. Expected 0, got 3.")
end

it 'validates `with` args against the method signature when stubbing a method' do
dbl = class_double(LoadedClass)
prevents(/Wrong number of arguments. Expected 0, got 2./) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ module Mocks
prevents { o.__send__(:undefined_method) }
end

it 'verifies arguments' do
expect {
o.defined_instance_method(:too, :many, :args)
}.to raise_error(ArgumentError, "Wrong number of arguments. Expected 0, got 3.")
end

it "includes the double's name in a private method error" do
expect {
o.rand
Expand Down

0 comments on commit 04e048f

Please sign in to comment.