-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
Setup/Verifyable With Times #530
Comments
Duplicate of #373. |
@stakx sorry, i didn't search closed issues :( as an asside, for anyone else who comes here, this is my workaround/hack which is a superclass of a Mock which implments a setup and saves the verify to be called as part of the verify all. Granted, this doesn't actually go as far as to error in a strict mock on the additional execution, but will error at the end. This is a pure helper so you can setup and verify the same thing with times which for me is a happy middle case.
|
@stakx having read through #373 I actually don't believe this is a duplicate as the reasoning behind this is not the same. The point I am trying to make is 2 fold. The first is the ability to specify a return to only occur X number of times. The second is the ability to chain a setup with a verify in a single statement. In summary, the ability to state during setup, I want this to happen when the function is called X times. |
I don't believe the suggested syntax would be a very good fit with Moq's current API. It would basically turn everything into sequences. (And there are already two mechanisms for that, mock.SetupSequence(m => DoSth())
.Returns(true)
.Returns(false)
.Returns(...)
...
.Throws(new InvalidOperationException());
... (Yes, The syntax you're suggesting ( I don't see any way to implement your suggestion. I think we'd do better to focus on making Moq's sequence features (e.g. P.S.: There are also conditional setups ( int calls = 0;
mock.When(() => calls == 0).Setup(m => m.DoSth()).Returns(true).Callback(() => ++calls);
mock.When(() => calls < 6).Setup(m => m.DoSth()).Returns(false).Callback(() => ++calls);
...
Assert.Equal(6, calls); Be aware that conditional setups done this way won't be thread-safe. |
@stakx i didn't know about SetupSequence, that does exactly what i wanted for half of this issue :) The other half is essentially this.
having the setup and verify lines being 90% the same, but verify having the extra condition just seems very verbose and this feels a bit wrong
|
@BlythMeister - #527 is discussing a possibility of avoiding repetition in setup / verification expressions. Apart from that, the easiest way of avoiding identical setup and verification expressions would be to use a loose mock instead of a strict mock (if that's acceptable in your situation), because then you might not need the setup at all and would only be left with the verification expression. Another (albeit not very elegant) alternative would be to store the expression in a variable, then pass it to |
ok, i'll keep an eye out for #527 thanks. And yeah, my second example was the not very elegant scenario. |
Would it be possible to provide a setup with an expected number of times the execution occurs.
And then use verifyable and verify all to ensure that it was executed the correct number of times.
This would work in a similar way to RhinoMocks with
There is 2 reasons why you might want to do this.
Currently in moq you would need to:
If DoFunction took a lot of parameters, or something which is verbose, that's a lot of copied data between the setup and verify, just to make sure it was called x times and returned the right values on each of the 6 calls (true the first time, and false the other 5 times)
If this could be simplified to
The text was updated successfully, but these errors were encountered: