-
-
Notifications
You must be signed in to change notification settings - Fork 126
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
Pass counter to the generated methods (optionally, with -count flag) #160
Comments
I am personally not convinced, that the argument of having to split the declaration and the implementation is worth the additional complexity of the proposed flag. If you don't like the assignment of the functions one by one, you could even go for: var p1 *ProviderMock
p1 = &ProviderMock {
EventsFunc: func(ctx context.Context) <-chan ProviderID {
evCount := len(p1.EvensCalls())
....
},
} |
well, this doesn't resolve the split, i.e. declaration in your example is still a separate step from the actual implementation. Personally, I think the simpler "user interface" is worths it, as we get rid of this artificial split and won't need the len call for something that essential and useful. From my experience, many non-trivial tests need some sort of state, and having this count easily accessible will make usage of moq more pleasant. I don't think the implementation will add any significant amount of code either. |
In non trivial test, where my mock needs to respond differently in multiple subsequent calls (where the number of calls would become handy), I normally use a pattern with a list (slice) of responses (could also be a list of functions that return the necessary response values) where on every request, the first item is taken from the list. This looks more or less like this: type Response string
responseQueue := []State{ "initial", "running", "finished" }
p1 := &ProviderMock {
EventsFunc: func(ctx context.Context) Response {
is.True(len(responseQueue) > 0) // EventsFunc was called when queue was depleted
// Pop response
resp := responseQueue[0]
responseQueue = responseQueue[1:]
return resp
},
} In my case, the responseQueue is often part of my test table.
|
thx, this is an interesting approach. I have tried to implement POV and it wasn't hard to make it generate what I wanted. However, I missed an essential issue - as soon as I add an extra parameter |
Thank you @breml for the helpful suggestions 🙂 |
Very often I need to alter the return value of the called mock function based on the sequence number (counter). Currently, it is possible to do this by maintaining an external count and incrementing it in the call itself. I mean smth like this:
Another way to achieve it is to use the provided FuncCalls() method, I.e. len(FuncCalls()) but this approach requires to split declaration and implementation, I.e one needs to declare mock first and add the function after this:
My proposal is to add an extra flag, something like
-count
adding a count parameter to each generated mock and the usage will be as simple as this:Let me know what you think of this idea and I could try to implement it if you like it.
The text was updated successfully, but these errors were encountered: