-
Notifications
You must be signed in to change notification settings - Fork 84
Proposal: clearer calls assertions with pattern matching #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@Olshansk any thoughts? I'd be happy to prepare a PR (incl proper documentation etc) but I prefer not to do the work if you don't like the API. |
Hey, sorry for the delay on this... Overall, I like the proposal and think we should move forward with it.
Just noteworthy: Erlang's meck (what this uses) has a num_calls function. In a separate PR, we could build a wrapper around it to expose it in an elixir native way.
Let's go with
Let's avoid looking at the return values for the purpose of this feature. |
Hi there,
First off, thanks for Mock, it's super nice. Now, I read #109 but I think I have a better solution. I wanted to discuss the API a bit before I turn it into a PR.
Basically, besides the lack of pattern matching there's something more that bothers me about
assert_called
: it asserts that the function has been called at least once. That means you don't catch bugs where a function is called too often. I'd prefer to be able to verify that a function is called exactly as many times as I expected, with exactly the parameters I care about.The good news is, I think I wrote a macro that solves this:
It's my first Elixir macro so let me know if I did something stupid :-)
Call it like this:
This returns a list of all calls that were made to
MyModule.somefunc
with parameters that match the parameters shown. Eg if the software under test calls this:then the
calls
call above returns this list, the parameters for each call:Now, that's a lovely list for a bog standard
assert
, which allows pattern-matched assignments. Eg:What do you think about this? I'll be happy to prepare a PR.
Note: personally I think I'd want to ship a second overload for the macro that accepts each part separately:
This lets you specify a pattern for the function name too, eg
calls(MyModule, _, _)
gets all calls of any arity to (mocked) functions in MyModule.If you like this approach I'll code that overload up as well and put them in the same PR. I'm also not 100% sure about the name
calls
. I consideredget_calls
too, andcall_history
, but they feel needlessly long, plus you gotcall_history
in master already (even though I feel like this macro would essentially supersede it).The final thing I'm not sure about is that this macro returns the parameter list, but not the return values. I don't think return values are often useful when asserting mock behavior, but maybe sometimes they are (esp when using
:passthrough
). Maybe it should return adefstruct [:params, :return_value]
instead of just the params.Curious about your thoughts!
The text was updated successfully, but these errors were encountered: