Closed nrxus closed 3 years ago
What is the most intuitive behavior on how this would handle mocks with different arguments.
when!(my_mock.some_method(3)).then_return(666)
when!(my_mock.some_method(3)).then_return(3)
when!(my_mock.some_method).then_return(666)
when!(my_mock.some_method(5)).then_return(5)
my_mock.some_method(3)
=> panic!
. Latest mock does not match the argument
my_mock.some_method(10)
=> panic!
. Latest mock does not match the argument
my_mock.some_method(5)
=> 5
. Latest mock matches the argument
This is the current behavior. Right now it is okay because mock methods don't take arguments.
my_mock.some_method(3)
=> 999
. Latest mock that handles the input is the closure.
my_mock.some_method(10)
=> 999
. Latest mock that handles the input is the closure.
my_mock.some_method(5)
=> 5
. Latest mock that handles the input is the specific mock.
my_mock.some_method(3)
=> 3
. Most specific mocks return 3
and 666
, with 3
being the latest mock.
my_mock.some_method(10)
=> 999
. Only mock that handles the argument.
my_mock.some_method(5)
=> 5
. Most specific mock.
These three have different trade offs in most seemingly "correct" vs complexity of both implementation and explanations (i.e., what does "specific" even mean?)
Another alternative:
Instead of when!
vs unchecked_when!
just add an unchecked
version to each method?
This has been fully implemented! Just waiting on tweaking docs to do a release.
This has been released in v0.0.9!
API Changes
Mocking with arguments
when!
can now accept "arguments" into the mocked methods, such as:Arguments can be partially specified:
or unspecified
when!
returns an object that exposes two methods:then_return
: returns a valuethen
: accepts a closure. If arguments are specified in the macro, they won't be visible in the closure.unchecked_when
Create a new
unchecked_when!
macro that is theunsafe
version ofwhen!
, exposing the same functionality. Useunchecked_when!
to mock methods with references in the arguments or output, as this cannot be done withoutunsafe
code.Thanks @muscovite for the 🧠