Moq vs TypeMock – Simple solution to It.IsAny

So I just posted this post where I wanted the It.IsAny syntax in Typemock. Without giving any long thought on this I wrote two simple solutions:

Solution 1

public static class IVerifyHandlerExtensions
{
    public static void WasCalledWithAnyArguments<T>(this IVerifyHandler handler, Action<T> action)
    {
        handler.WasCalledWithAnyArguments(() => action.Invoke(default(T)));
    }
}
[Test, Isolated]
public void TheTest()
{
    var fake = Isolate.Fake.Instance<MyClassUnderTest>();
    fake.MyMethod(new MyArgsWithoutEquals());

    Isolate.Verify.WasCalledWithAnyArguments<MyArgsWithoutEquals>(fake.MyMethod);
}

Solution 2

public static class IsAny<T>
{
    public static T Item = default(T);
}
[Test, Isolated]
public void TheTest2()
{
    var fake = Isolate.Fake.Instance<MyClassUnderTest>();
    fake.MyMethod(new MyArgsWithoutEquals());

    Isolate.Verify.WasCalledWithAnyArguments(() => fake.MyMethod(IsAny<IMyArgs>.Item));
}

As I said. Not much thinking in there so I’m probably missing something. It worked for me. Now I don’t have to type “null” any more.

//Daniel

2 thoughts on “Moq vs TypeMock – Simple solution to It.IsAny

  1. Pingback: Tweets that mention I put together a post instead. -- Topsy.com

  2. Pingback: Moq vs Typemock – Simple solution to It.Is(expression) « Daniel Wertheim

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s