Skip to content

Commit

Permalink
Add check of the arity of the mocked method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cainã Costa committed Oct 10, 2011
1 parent ff0e02b commit 0b9266b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -28,11 +28,10 @@ class MyOtherClassTest < MiniTest::Unit::TestCase
end
```

The only real difference of using `MiniTest::FireMock` instead of `MiniTest::Mock` is that if `MyClass` is defined, and `my_method` isn't there, it'll raise a `MockExpectationError`.
The only real difference of using `MiniTest::FireMock` instead of `MiniTest::Mock` is that if `MyClass` is defined, and `my_method` isn't there, it'll raise a `MockExpectationError`. It checks also for the arity of the method, so it'll raise a `MockExpectationError` if the real method have a different arity than the expectation.

TODO
----

- Check for the arity of the methods if they are defined.
- Mock class/module methods too.
- Make it work with method_missing (as of now it doesn't, even if the #responds_to? is correct)
4 changes: 4 additions & 0 deletions lib/minitest/fire_mock.rb
Expand Up @@ -14,6 +14,10 @@ def expect(name, retval, args = [])
raise MockExpectationError, "expected #{@constant_name} to define `#{name}`, but it doesn't"
end

if method and method.arity != args.size
raise MockExpectationError, "`#{name}` expects #{method.arity} arguments, given #{args.size}"
end

super(name, retval, args)
end

Expand Down
7 changes: 7 additions & 0 deletions test/unit/firemock_test.rb
Expand Up @@ -43,4 +43,11 @@ def test_mock_with_namespace_is_invalid_when_defined_but_dont_responds_to_method
mock.expect(:not_defined_method, 42)
end
end

def test_valid_mock_with_different_arity
mock = MiniTest::FireMock.new('DefinedConstant')
assert_raises MockExpectationError, "`defined_method` expects 0 arguments, given 3" do
mock.expect(:defined_method, 42, [1,2,3])
end
end
end

0 comments on commit 0b9266b

Please sign in to comment.