'Minitest how to solve mocked method :call expects 0 arguments, got 1
I have a service with the call method to stub. But its always have this error:
SomeTest#test_1:
ArgumentError: mocked method :call expects 0 arguments, got 1
test.rb:17:in `block in test_1'
test.rb:16:in `test_1'
Here's the code:
require 'minitest/autorun'
class SomeService
def initialize(headers = {})
end
def call
end
end
class SomeTest < Minitest::Test
def test_1
mock = Minitest::Mock.new
mock.expect :call, nil
SomeService.stub :new, mock do
SomeService.new({}).call
end
end
end
What did I do wrong? thanks
Solution 1:[1]
In general what you wrote is right but you hit and edge case with the method name call.
As you can see in the documentation of stub method.
"If val_or_callable responds to #call, then it returns the result of calling it".
So basically the problem is your mock responds to call, and the stub method is calling it before entering the block.
You can solve it by wrapping the mock in a lambda that discards the arguments.
require 'minitest/autorun'
class SomeService
def initialize(headers = {})
end
def call
end
end
class SomeTest < Minitest::Test
def test_1
mock = Minitest::Mock.new
mock.expect :call, nil
SomeService.stub :new, ->(_args) { mock } do
SomeService.new({}).call
end
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | javiyu |
