AutoFixture, xUnit.net, and Auto Mocking
The features discussed in this post are available when using AutoFixture decleratively with the xUnit.net extension. In addition, it is required to have at least one of the extensions for Auto Mocking using AutoMoq, AutoRhinoMocks, or AutoFakeItEasy.
To install AutoFixture with xUnit.net data theories, run the following command in the Package Manager Console:
PM> Install-Package AutoFixture.Xunit
In the test method below we would like to use xUnit.net data theories to provide:
- Auto-generated data specimens for
d,s,i,a. - Auto-generated data specimens for
_,s,i,a, and inline values ford. - Auto-generated data specimens for
_,_,i,a, and inline values ford,s.
public void TestMethod(
double d, string s, IInterface i, AbstractType a)
{
Assert.True(
d != 0 &&
s != null &&
i != null &&
a != null
);
}
That fact that argument
iis an interface and argumentais an abstract class prevents us from using[InlineData],[AutoData], or[InlineAutoData]attributes.
AutoFixture allows us to easily define custom DataAttribute derived types:
- By deriving from AutoDataAttribute and passing to the base constructor a customization that enables auto mocking with Moq, Rhino Mocks, or FakeItEasy.
- By deriving from CompositeDataAttribute and passing to the base constructor an array of DataAttribute derived type instances, e.g. an instance of
InlineDataAttributeand an instance ofAutoDataAttributederived type.
Auto Mocking using Moq
To install AutoFixture with Auto Mocking using Moq, run the following command in the Package Manager Console:
PM> Install-Package AutoFixture.AutoMoq
We decorate the test method with AutoMoqData and InlineAutoMoqData attributes:
[Theory]
[AutoMoqData]
[InlineAutoMoqData(2)]
[InlineAutoMoqData(3, "foo")]
public void WithMoq(
double d, string s, IInterface i, AbstractType a)
{
Write(d, s, i, a);
}
Output:
AutoMoqData:
d = 1
s = s05080d05-b874-4182-bba5-18678ad9b134
i = Castle.Proxies.IInterfaceProxy
a = Castle.Proxies.AbstractTypeProxy
InlineAutoMoqData:
d = 2
s = sce87888d-8cc8-4f69-927c-c4b1347d3147
i = Castle.Proxies.IInterfaceProxy
a = Castle.Proxies.AbstractTypeProxy
InlineAutoMoqData:
d = 3
s = foo
i = Castle.Proxies.IInterfaceProxy
a = Castle.Proxies.AbstractTypeProxy
Source code:
internal class AutoMoqDataAttribute : AutoDataAttribute
{
internal AutoMoqDataAttribute()
: base(new Fixture().Customize(new AutoMoqCustomization()))
{
}
}
internal class InlineAutoMoqDataAttribute : CompositeDataAttribute
{
internal InlineAutoMoqDataAttribute(params object[] values)
: base(new DataAttribute[] {
new InlineDataAttribute(values), new AutoMoqDataAttribute() })
{
}
}
More information on AutoFixture with Auto Mocking using Moq can be found at AutoFixture as an auto-mocking container blog post.
Auto Mocking using Rhino Mocks
To install AutoFixture with Auto Mocking using Rhino Mocks, run the following command in the Package Manager Console:
PM> Install-Package AutoFixture.AutoRhinoMocks
We decorate the test method with AutoRhinoMockData and InlineAutoRhinoMockData attributes:
[Theory]
[AutoRhinoMockData]
[InlineAutoRhinoMockData(2)]
[InlineAutoRhinoMockData(3, "foo")]
public void WithRhinoMocks(
double d, string s, IInterface i, AbstractType a)
{
Write(d, s, i, a);
}
Ouput:
AutoRhinoMockData:
d = 1
s = s01172643-5162-474f-bcfa-319ef40a8272
i = Castle.Proxies.IInterfaceProxy201f58a5559841bfb41895881489658c
a = Castle.Proxies.AbstractTypeProxy5a86172bbda14cc098b4c675c5b7e555
InlineAutoRhinoMockData:
d = 2
s = sd3b9c112-5181-4daa-9b2f-333c7498044a
i = Castle.Proxies.IInterfaceProxy201f58a5559841bfb41895881489658c
a = Castle.Proxies.AbstractTypeProxy5a86172bbda14cc098b4c675c5b7e555
InlineAutoRhinoMockData:
d = 3
s = foo
i = Castle.Proxies.IInterfaceProxy201f58a5559841bfb41895881489658c
a = Castle.Proxies.AbstractTypeProxy5a86172bbda14cc098b4c675c5b7e555
Source code:
internal class AutoRhinoMockDataAttribute : AutoDataAttribute
{
internal AutoRhinoMockDataAttribute()
: base(new Fixture().Customize(new AutoRhinoMockCustomization()))
{
}
}
internal class InlineAutoRhinoMockDataAttribute : CompositeDataAttribute
{
internal InlineAutoRhinoMockDataAttribute(params object[] values)
: base(new DataAttribute[] {
new InlineDataAttribute(values), new AutoRhinoMockDataAttribute() })
{
}
}
More information on AutoFixture with Auto Mocking using Rhino Mocks can be found at Rhino Mocks-based auto-mocking with AutoFixture blog post.
Auto Mocking using FakeItEasy
To install AutoFixture with Auto Mocking using FakeItEasy, run the following command in the Package Manager Console:
PM> Install-Package AutoFixture.AutoFakeItEasy
We decorate the test method with AutoFakeItEasyData or InlineAutoFakeItEasyData attributes:
[Theory]
[AutoFakeItEasyData]
[InlineAutoFakeItEasyData(2)]
[InlineAutoFakeItEasyData(3, "foo")]
public void WithFakeItEasy(
double d, string s, IInterface i, AbstractType a)
{
Write(d, s, i, a);
}
Output:
AutoFakeItEasyData:
d = 1
s = s2b4c118b-d18b-4782-992a-d4138c65fd2a
i = Faked IInterface
a = Faked AbstractType
InlineAutoFakeItEasyData:
d = 2
s = se8da7e09-ea4f-4d16-86bd-4229802e5f5d
i = Faked IInterface
a = Faked AbstractType
InlineAutoFakeItEasyData:
d = 3
s = foo
i = Faked IInterface
a = Faked AbstractType
Source code:
internal class AutoFakeItEasyDataAttribute : AutoDataAttribute
{
internal AutoFakeItEasyDataAttribute()
: base(new Fixture().Customize(new AutoFakeItEasyCustomization()))
{
}
}
internal class InlineAutoFakeItEasyDataAttribute : CompositeDataAttribute
{
internal InlineAutoFakeItEasyDataAttribute(params object[] values)
: base(new DataAttribute[] {
new InlineDataAttribute(values), new AutoFakeItEasyDataAttribute() })
{
}
}
More information on AutoFixture with Auto Mocking using FakeItEasy can be found at Auto-Mocking with FakeItEasy and AutoFixture blog post.