Permalink
Browse files

Add check of the arity of the mocked method.

  • Loading branch information...
1 parent ff0e02b commit 0b9266b941a0c7bbeb3d4e17e961088b24e00e64 @cfcosta committed Oct 10, 2011
Showing with 12 additions and 2 deletions.
  1. +1 −2 README.md
  2. +4 −0 lib/minitest/fire_mock.rb
  3. +7 −0 test/unit/firemock_test.rb
View
@@ -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)
@@ -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
@@ -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.