-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
A verify method that returns a boolean. #287
Comments
This is more of an issue with xUnit than with Moq. You could do the following, if you really wanted to: var ex = Record.Exception(() => mock.Verify());
Assert.Null(ex); Does that solve your issue? |
I know but still, very ugly. I'd like an IsVerified bool property or something but I'll live without it ;) |
How about creating your own extension method? Something along the lines of: using System;
using System.Linq.Expressions;
using Moq;
static class MockExtensions
{
public static bool TryVerify<T>(
this Mock<T> mock,
Expression<Action<T>> expression, // delegate type according to Verify overload you want
…, // additional parameters according to Verify overload you want
out Exception error) // you might not be interested in this bit
{
try
{
mock.Verify(expression, …);
error = null;
return true;
}
catch (Exception ex)
{
error = ex;
return false;
}
}
} Which you'd then use as follows: var mock = new Mock<Foo>();
…
bool isVerified = mock.TryVerify(foo => …, …, out Exception error);
Assert.True(isVerified); |
Well, I think that the Action<T> isn't even necessary :)
On Sat, Jun 3, 2017 at 6:17 PM stakx ***@***.***> wrote:
How about rolling your own in the form of an extension method? Something
along the lines of:
static class MockExtensions
{
public static bool Verifies<T>(this Mock<T> mock, Action<T> …)
{
try
{
mock.Verify(…);
return true;
}
catch (MockException)
{
return false;
}
}
}
Which you'd then use as follows:
var mock = new Mock<Foo>();Assert.True(mock.Verifies(foo => …));
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#287 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAKW67DTOoKlwrog7sr1hAFUfqo28w27ks5sAc1agaJpZM4KAiN4>
.
--
…--
/kzu from mobile
|
Alright thanks, commenting on your example I'd use tuples but ok, no bool method coming up. Closing this |
Xunit 2 does not know the concept of NotThrows. I'd like to Assert something in my tests.
Verify() returns void or an exception. So in the case that I'd like to Assert something indeed was called according to Setup() I cannot Assert this because Verify() does not return a result.
Now I can do :
But that's a bit ugly don't you think?
The text was updated successfully, but these errors were encountered: