Skip to content

Instantly share code, notes, and snippets.

@JefStat
Forked from ngocvantran/MoqExtensions.cs
Created May 7, 2018 13:22
Show Gist options
  • Select an option

  • Save JefStat/5e0872d137f81b76b31b1885de01ce4e to your computer and use it in GitHub Desktop.

Select an option

Save JefStat/5e0872d137f81b76b31b1885de01ce4e to your computer and use it in GitHub Desktop.

Revisions

  1. 7Pass revised this gist May 20, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MoqExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ public static void VerifyIgnoreArgs<T>(this Mock<T> mock,
    expression = new MakeAnyVisitor().VisitAndConvert(
    expression, "VerifyIgnoreArgs");

    mock.Verify(expression, times ?? Times.Once);
    mock.Verify(expression, times ?? Times.AtLeastOnce);
    }

    public static void VerifyIgnoreArgs<T, TResult>(this Mock<T> mock,
    @@ -43,7 +43,7 @@ public static void VerifyIgnoreArgs<T, TResult>(this Mock<T> mock,
    expression = new MakeAnyVisitor().VisitAndConvert(
    expression, "VerifyIgnoreArgs");

    mock.Verify(expression, times ?? Times.Once);
    mock.Verify(expression, times ?? Times.AtLeastOnce);
    }

    private class MakeAnyVisitor : ExpressionVisitor
  2. 7Pass created this gist May 20, 2014.
    64 changes: 64 additions & 0 deletions MoqExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    using System;
    using System.Linq.Expressions;
    using Moq.Language.Flow;

    namespace Moq
    {
    public static class MoqExtensions
    {
    public static ISetup<T, TResult> SetupIgnoreArgs<T, TResult>(this Mock<T> mock,
    Expression<Func<T, TResult>> expression)
    where T : class
    {
    expression = new MakeAnyVisitor().VisitAndConvert(
    expression, "SetupIgnoreArgs");

    return mock.Setup(expression);
    }

    public static ISetup<T> SetupIgnoreArgs<T>(this Mock<T> mock,
    Expression<Action<T>> expression)
    where T : class
    {
    expression = new MakeAnyVisitor().VisitAndConvert(
    expression, "SetupIgnoreArgs");

    return mock.Setup(expression);
    }

    public static void VerifyIgnoreArgs<T>(this Mock<T> mock,
    Expression<Action<T>> expression, Func<Times> times = null)
    where T : class
    {
    expression = new MakeAnyVisitor().VisitAndConvert(
    expression, "VerifyIgnoreArgs");

    mock.Verify(expression, times ?? Times.Once);
    }

    public static void VerifyIgnoreArgs<T, TResult>(this Mock<T> mock,
    Expression<Func<T, TResult>> expression, Func<Times> times = null)
    where T : class
    {
    expression = new MakeAnyVisitor().VisitAndConvert(
    expression, "VerifyIgnoreArgs");

    mock.Verify(expression, times ?? Times.Once);
    }

    private class MakeAnyVisitor : ExpressionVisitor
    {
    protected override Expression VisitConstant(ConstantExpression node)
    {
    if (node.Value != null)
    return base.VisitConstant(node);

    var method = typeof(It)
    .GetMethod("IsAny")
    .MakeGenericMethod(node.Type);

    return Expression.Call(method);
    }
    }
    }
    }
    89 changes: 89 additions & 0 deletions MoqExtensionsTests.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Xunit;

    namespace Moq
    {
    public class MoqExtensionsTests
    {
    [Fact]
    public void SetupIgnoreArgs_action_should_ignore_arguments()
    {
    var mock = new Mock<IDummy>();
    mock
    .SetupIgnoreArgs(x => x
    .Execute(null, null, null))
    .Verifiable();

    mock.Object.Execute("dummy input",
    new[] {"dummy value"},
    new Dictionary<string, object>
    {
    {"dummy key", new object()},
    });

    mock.Verify();
    }

    [Fact]
    public void SetupIgnoreArgs_function_should_ignore_arguments()
    {
    var mock = new Mock<IDummy>();
    mock
    .SetupIgnoreArgs(x => x
    .Get(null, null, null))
    .Verifiable();

    mock.Object.Get("dummy input",
    new[] {"dummy value"},
    new Dictionary<string, object>
    {
    {"dummy key", new object()},
    });

    mock.Verify();
    }

    [Fact]
    public void VerifyIgnoreArgs_action_should_ignore_arguments()
    {
    var mock = new Mock<IDummy>();

    mock.Object.Execute("dummy input",
    new[] {"dummy value"},
    new Dictionary<string, object>
    {
    {"dummy key", new object()},
    });

    mock.VerifyIgnoreArgs(x => x
    .Execute(null, null, null));
    }

    [Fact]
    public void VerifyIgnoreArgs_function_should_ignore_arguments()
    {
    var mock = new Mock<IDummy>();

    mock.Object.Get("dummy input",
    new[] {"dummy value"},
    new Dictionary<string, object>
    {
    {"dummy key", new object()},
    });

    mock.VerifyIgnoreArgs(x => x
    .Get(null, null, null));
    }

    public interface IDummy
    {
    void Execute(string input, IList values,
    IDictionary<string, object> settings);

    string Get(string input, IList values,
    IDictionary<string, object> settings);
    }
    }
    }