Skip to content
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

Closed
NinoFloris opened this issue Sep 19, 2016 · 5 comments
Closed

A verify method that returns a boolean. #287

NinoFloris opened this issue Sep 19, 2016 · 5 comments

Comments

@NinoFloris
Copy link

NinoFloris commented Sep 19, 2016

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 :

mock.Verify();
Assert.True(true);

But that's a bit ugly don't you think?

@stakx
Copy link
Contributor

stakx commented May 31, 2017

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?

@NinoFloris
Copy link
Author

I know but still, very ugly. I'd like an IsVerified bool property or something but I'll live without it ;)

@stakx
Copy link
Contributor

stakx commented Jun 3, 2017

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);

@kzu
Copy link
Contributor

kzu commented Jun 4, 2017 via email

@NinoFloris
Copy link
Author

Alright thanks, commenting on your example I'd use tuples but ok, no bool method coming up. Closing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants