AutoFakeItEasy
FakeItEasy is a .NET mocking library. You create fakes for interfaces and abstract types, then configure behavior and assert calls.
AutoFixture.AutoFakeItEasy plugs FakeItEasy into AutoFixture. When AutoFixture builds an object graph and needs an interface or abstract dependency, AutoFakeItEasy supplies a fake instead of failing — so collaborators resolve automatically and you can assert on them with less manual setup.
Prerequisites
- .NET 8+ test project
- AutoFixture 5.0.0-rc.1
- FakeItEasy
Install
Install AutoFixture.AutoFakeItEasy and add an explicit FakeItEasy package reference in the same test project.
Pin matching versions of AutoFixture and AutoFixture.AutoFakeItEasy. Reference the latest FakeItEasy yourself when you update packages.
<PackageReference Include="AutoFixture" Version="5.0.0-rc.1" />
<PackageReference Include="AutoFixture.AutoFakeItEasy" Version="5.0.0-rc.1" />
<PackageReference Include="FakeItEasy" Version="9.0.0" />
Example scenario
You are testing OrderService with FakeItEasy. You need a fake IOrderRepository injected into the service and want to assert Save was called. Other tests need fake methods to return fixture values, or FakeItEasy-backed delegates.
Basic usage
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
var repository = fixture.Freeze<IOrderRepository>();
var service = fixture.Create<OrderService>();
var order = fixture.Create<Order>();
service.PlaceOrder(order);
A.CallTo(() => repository.Save(order)).MustHaveHappenedOnceExactly();
Create<OrderService>() injects a fake IOrderRepository. Freeze<IOrderRepository>() keeps one fake so you can assert with A.CallTo.
Combine with xUnit.net 3 via a custom AutoData attribute when tests use [AutoData].
Fake wrappers and interface requests
AutoFakeItEasy registers builders for both Fake<T> and the faked type T (interfaces and abstract classes). Freezing the interface is usually enough for A.CallTo:
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
var repository = fixture.Freeze<IOrderRepository>();
var service = fixture.Create<OrderService>();
Freeze or create Fake<T> when you need the FakeItEasy wrapper (for example FakedObject or rules on the fake):
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
var repository = fixture.Freeze<Fake<IOrderRepository>>();
var service = fixture.Create<OrderService>();
var order = fixture.Create<Order>();
service.PlaceOrder(order);
A.CallTo(() => repository.FakedObject.Save(order)).MustHaveHappenedOnceExactly();
Create<Fake<T>>() builds a configured Fake<T> when you want the wrapper without freezing.
Configuration options
AutoFakeItEasyCustomization exposes properties you set before calling Customize. Defaults are all off / default relay:
| Property | Default | Effect |
|---|---|---|
ConfigureMembers | false | When true, fake members return values from the fixture |
GenerateDelegates | false | When true, FakeItEasy creates delegates (Func, Action, …) |
Relay | FakeItEasyRelay | Residue collector that turns interface/abstract requests into fakes |
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization
{
ConfigureMembers = true,
GenerateDelegates = true
});
ConfigureMembers
With the default (ConfigureMembers = false), FakeItEasy uses its usual defaults — methods return type defaults unless you configure them:
public interface IGreeter
{
string Greet();
}
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
var greeter = fixture.Create<IGreeter>();
Assert.Equal(string.Empty, greeter.Greet());
With ConfigureMembers = true, AutoFixture configures fake members so calls return specimens from the fixture:
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization
{
ConfigureMembers = true
});
var order = fixture.Freeze<Order>();
var repository = fixture.Create<ICommerceOrderRepository>();
Assert.Same(order, repository.GetById(1));
Use ConfigureMembers when the SUT reads from collaborators and you want anonymous return values without configuring every member. Prefer the default when you only care about verifying calls (as in the basic A.CallTo example).
ConfigureMembers sets up virtual members (including out / ref where FakeItEasy allows it) and can assign sealed members from the fixture. It does not configure:
- Non-virtual instance members that FakeItEasy cannot intercept
- Static members
- Members that are not overridable in a way FakeItEasy can fake
Configure those members with FakeItEasy when you need explicit return values.
GenerateDelegates
By default, AutoFixture's own kernel creates delegates. With GenerateDelegates = true, FakeItEasy creates them instead — useful when you want fake or auto-configured delegates:
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization
{
GenerateDelegates = true,
ConfigureMembers = true
});
var expected = fixture.Freeze<string>();
var format = fixture.Create<Func<int, string>>();
Assert.Equal(expected, format(42));
Without ConfigureMembers, the FakeItEasy delegate still exists but return values stay at FakeItEasy defaults unless you configure them.
Relay
Relay is the specimen builder added to fixture.ResidueCollectors. The default FakeItEasyRelay answers requests for interfaces and abstract types with FakeItEasy fakes.
Most tests leave the default. Replace it only when you need a custom residue collector — for example a FakeItEasyRelay with a different request specification:
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization
{
Relay = new FakeItEasyRelay(new ExactTypeSpecification(typeof(IOrderRepository)))
});
That example would fake only IOrderRepository requests that match the specification you supply; other abstractions follow the rest of the fixture pipeline.
How it works
AutoFakeItEasyCustomization— registers FakeItEasy fake creation and a residue collector for abstractionsConfigureMembers— auto-configure fake members from the fixture (with known gaps — see above)GenerateDelegates— create delegates with FakeItEasy instead of the AutoFixture kernelRelay— residue collector (defaultFakeItEasyRelay) for interface/abstract requestsFreeze<T>()/Freeze<Fake<T>>()— one shared fake (or FakeItEasy wrapper) for setup and verification