I'm using AutoFixture, Moq, and XUnit extensions ([Theory]
attribute) as described in this blog post http://blog.ploeh.dk/2010/10/08/AutoDataTheorieswithAutoFixture.
I've noticed that most of unit tests look like this:
[Theory, AutoMoqData]
public void Test(
[Frozen] Mock<IServiceOne> serviceOne,
[Frozen] Mock<IServiceTwo> serviceTwo,
MyClass classUnderTest)
{
// Arrange
serviceOne
.Setup(m => m.Get(It.IsAny<int>()));
serviceTwo
.Setup(m => m.Delete(It.IsAny<int>()));
// MyClass has a constructor with arguments for IServiceOne, and IServiceTwo
// classUnderTest will use the two mocks specified above
// Act
var result = classUnderTest.Foo();
// Assert
Assert.True(result);
}
As opposed to always decorating the mocks with [Frozen]
, is there a way to setup the fixture to always freeze mocks?
Here's the AutoMoqData
attribute:
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute()
: base(new Fixture().Customize(new AutoMoqCustomization()))
{
}
}
fixture.Customizations.Add( new MemoizingBuilder( new NSubstituteBuilder( new MethodInvoker( new NSubstituteMethodQuery()))));
– Seringapatam