patch icon indicating copy to clipboard operation
patch copied to clipboard

Ability to assert arguments of specific function invocation

Open Arsenalist opened this issue 1 year ago • 1 comments

For the situation where a function is called 10 times, is there a way to assert what args the 6th invocation of the function was called with?

assert_called(MyModule.my_function(session_arg), 10)

In this example session_arg will bind to the last matching call, which allows me to do asserts against that. However, I want to assert the 6th call. Is this possible, and if not, could it be added as a feature?

Arsenalist avatar Mar 24 '24 10:03 Arsenalist

For more advanced use cases like this you can make use of history/2 https://hexdocs.pm/patch/Patch.html#history/2 which provides all the calls that have been seen. So for example you could do

{:my_function, [session_arg]} = 
  MyModule
  |> history()
  |> Enum.filter(fn {func, args} -> func == :my_function end)
  |> Enum.at(5)

assert session_arg == expected_session_arg

ihumanable avatar Mar 25 '24 17:03 ihumanable

@Arsenalist I'm going to close this out as answered, but if you still require assistance please feel free to open a new issue.

ihumanable avatar Aug 10 '24 00:08 ihumanable