Home

Awesome

<img src="/src/icon.png" height="30px"> Verify.Moq

Discussions Build status NuGet Status

Adds Verify support for verifying Moq types.

See Milestones for release notes.

NuGet package

https://nuget.org/packages/Verify.Moq/

Usage

<!-- snippet: Enable -->

<a id='snippet-Enable'></a>

[ModuleInitializer]
public static void Init() =>
    VerifyMoq.Initialize();

<sup><a href='/src/Tests/ModuleInitializer.cs#L3-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-Enable' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Given an interface:

<!-- snippet: ITarget.cs -->

<a id='snippet-ITarget.cs'></a>

public interface ITarget
{
    string Method(int a, int b);
}

<sup><a href='/src/Tests/ITarget.cs#L1-L4' title='Snippet source file'>snippet source</a> | <a href='#snippet-ITarget.cs' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

The Mock and its invocations can then be verified:

<!-- snippet: ReceivedCalls -->

<a id='snippet-ReceivedCalls'></a>

[Fact]
public Task Test()
{
    var mock = new Mock<ITarget>();

    mock.Setup(_ => _.Method(It.IsAny<int>(), It.IsAny<int>()))
        .Returns("response");

    var target = mock.Object;
    target.Method(1, 2);
    return Verify(mock);
}

<sup><a href='/src/Tests/Tests.cs#L3-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-ReceivedCalls' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Will result in:

<!-- snippet: Tests.Test.verified.txt -->

<a id='snippet-Tests.Test.verified.txt'></a>

[
  {
    Method: ITarget.Method(int a, int b),
    Arguments: [
      1,
      2
    ],
    ReturnValue: response
  }
]

<sup><a href='/src/Tests/Tests.Test.verified.txt#L1-L10' title='Snippet source file'>snippet source</a> | <a href='#snippet-Tests.Test.verified.txt' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->