AutoFixture and free Moq syntax

I've been using Moq for a while now, and for brevity, I most often express settings using fluent syntax Mock.Of

...

var foo = Mock.Of<IFoo>(f => f.Method(It.IsAny<string>()) == 7 && f.Property == "Hi");
var sut = new Whatever(foo);

      

I recently started playing with AutoFixture and don't seem to find an equivalent syntax for expressing multiple settings at the same time. I understand that I can express the same using Freeze

...

var foo = fixture.Freeze<Mock<IFoo>>();
foo.Setup(f => f.Method(It.IsAny<string>()).Returns(7);
foo.Setup(f => f.Property).Returns("Hi");
var sut = fixture.Create<Whatever>();

      

... but if at all possible, I would like to take advantage of the auto-bridge and keep it concise in Moq's smooth settings. Stylistic arguments aside, does AutoFixture do anything so that I can express those settings freely? If not, is there any optimization I can use to make the AutoFixture settings more concise?

+3


source to share


2 answers


You can create the layout yourself using the free Moq API and then push it back into the fixture:

var foo = Mock.Of<IFoo>(f => f.Method(It.IsAny<string>()) == 7 && f.Property == "Hi");
fixture.Inject(foo);

var sut = fixture.Create<Whatever>();

      



If you are using AutoMoqCustomization

then the code above will do the same as the code you posted, except for the not set CallBase = true

and DefaultValue = DefaultValue.Mock;

.

If you are using AutoConfiguredMoqCustomization

then this code will not set any additional members whereas the setting will. If you want to take advantage of AutoConfiguredMoqCustomization

and use the free Moq API, I'm afraid that's not possible.

+4


source


Riffing on @ dcastro's answer, a little wrapper that will have the same caveats as there: -

public static T SetMock<T>(this IFixture that, Expression<Func<T, bool>> expr)
    where T : class
{
    var mocked = Mock.Of(expr);
    that.Inject(mocked);
    return mocked;
}

      

Allows:

fixture.SetMock<IFoo>(f => 
    f.Method(It.IsAny<string>()) == 7 
    && f.Property == "Hi");

      

Or, if you are interested in a specific meaning:

var foo = fixture.SetMock<IFoo>(f => 
    f.Method(It.IsAny<string>()) == 7 
    && f.Property == "Hi");

      

Cleanest C # without helpers:



fixture.Inject( Mock.Of<IFoo>( f => 
    f.Method(It.IsAny<string>()) == 7 
    && f.Property == "Hi")));

      

And if you run your tests in F # , Foq , you should write it like:

Mock<IFoo>.With(fun x ->
    <@  x.Method(any()) --> 7
        x.Property --> "Hi" @>)
|> fixture.Inject

      

(the expression to the right of -->

is just code, i.e. you can put some algorithmic stuff in there and you get intellisense and type checking as you write it). Or (still with Foq):

fixture.Inject <|
    Mock<IFoo>.With(fun x -> <@  
        x.Method(any()) --> 7
        x.Property --> "Hi" @>)

      

Or (assuming it makes sense to implement the entire interface), you can use F # object expressions:

fixture.Inject { new IFoo with 
    member x.Method _ = 7 
    member x.Property with get() = "Hi" }

      

+4


source







All Articles