Search code examples
ruby-on-railsruby-on-rails-5minitest

Minitest mock not handling named parameters


I have a line of code:

@coverage_class.where(original_id: coverage.id, waiting_for_doc_requests: false)

and my unit test is doing the following expect:

mock = Minitest::Mock.new
mock.expect(:where, :results, [{ original_id: :id, waiting_for_doc_requests: false }])

and when running the unit test I get the following error:

Minitest::UnexpectedError: ArgumentError: mocked method :where expects 1 arguments, got []

The parameter array is not accepting the hash as an argument. All my other calls that do not use named parameters work fine.

Surely this can't be a limitation from Minitest? Named parameters in Ruby are pretty common nowadays.

Any help would be appreciated!


Solution

  • The method signature for expect is:

    def expect name, retval, args = [], **kwargs, &blk
    

    Docs and Source Code.

    Right now you are passing what should be keyword arguments as a positional argument of { original_id: :id, waiting_for_doc_requests: false }.

    So you can just change your expectation to

    mock.expect(:where, :results, original_id: :id, waiting_for_doc_requests: false)