-
-
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
Return matched invocation from Verify
#1117
Comments
There are a few things, apart from this being a breaking binary change, that I don't like about this:
I recently suggested a helper method |
I respect the desire to keep Most of my assertions follow the structure:
As an example, assume I am testing some kind of command-processing system. I want to know that a command was executed with a specified target:
Once I know I have the command I'm looking for, I want to verify the command object itself meets expectations. Currently I have to dig out the matching invocation, repeating myself (making the tests significantly less-maintainable) and then doing some ugly casts to the expected argument types.
|
You can use var commands = new List<FooCommand>();
mockProcessor.Verify(p => p.ExecuteAsync(Capture.In(commands, cmd => cmd.Target == "Bar")));
foreach (var command in commands)
{
// run additional checks on the captured commands
} #968 taught me that little trick. 😃 |
I tried this approach and it works pretty well, except my scenarios typically want to deal with exactly one capture, not a collection. However, I can't think of any syntax that would be simpler which is not forbidden by expressions (such as assigning to an out or ref parameter). |
@Tragedian you could create some kind of helper around a class Last<T>
{
public T Value;
public T Capture()
{
return Moq.Capture.With(new CaptureMatch<T>(arg => Value = arg, arg => true));
}
public T Capture(Predicate<T> predicate)
{
return Moq.Capture.With(new CaptureMatch<T>(arg => Value = arg, predicate));
}
} Then use it as follows: var lastCommand = new Last<FooCommand>();
mockProcessor.Verify(p => p.ExecuteAsync(lastCommand.Capture())); I'll leave it up to you to decide whether that's better than capturing into a collection using |
Having said that, let us close this issue. I think you've got a reasonably clean way of doing what you need to do without inspecting |
When doing more complex verification, particularly where complex objects are passed to methods, I want to be able to examine the arguments passed to a method in more detail.
Currently I can use the
Invocations
collection on the mock to find the relevant invocation. In scenarios where multiple invocations have been performed on the mock, this requires doing a search over the collection to find the matching invocation. Since we've already done that work inVerify
, it would be very convenient if that method just returned the matching invocation:This would be a "breaking" change to the signature of
Verify
, but since it currently has avoid
return, this would not affect most existing code.The text was updated successfully, but these errors were encountered: