FakeItEasy auto mocking integration for Autofac.
Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.
The primary entry point for this integration is the Autofac.Extras.FakeItEasy.AutoFake class. Once you create an AutoFake, you can Resolve the class under test and Autofac will automatically fill its constructor dependencies with FakeItEasy fakes. You then configure and assert on those fakes the way you normally would with FakeItEasy.
[Fact]
public void DependenciesAreAutomaticallyFaked()
{
using (var fake = new AutoFake())
{
// Configure the fake dependency that will be injected.
A.CallTo(() => fake.Resolve<IDependency>().GetValue()).Returns("expected value");
// Resolve the system under test; its IDependency is the fake configured above.
var sut = fake.Resolve<SystemUnderTest>();
// Act and assert as usual.
Assert.Equal("expected value", sut.DoWork());
A.CallTo(() => fake.Resolve<IDependency>().GetValue()).MustHaveHappened();
}
}To inject a specific instance or implementation instead of an automatic fake, register it through the configureAction constructor argument:
var dependency = new Dependency();
using (var fake = new AutoFake(configureAction: cfg => cfg.RegisterInstance(dependency).As<IDependency>()))
{
// SystemUnderTest receives your dependency instance.
var sut = fake.Resolve<SystemUnderTest>();
}See the documentation for more usage details and options.
Version 8.0.0 removed the AutoFake.Provide<TService>(instance) and AutoFake.Provide<TService, TImplementation>() methods. Each Provide call started a new lifetime scope, which meant a fake resolved before the call was a different instance than the one injected afterward - so configuration applied to the earlier fake silently had no effect.
Register specific dependencies through the configureAction constructor argument instead. Everything is registered into a single scope before the container is built, so the instances you configure are the instances that get injected.
// Before (7.0.0)
using (var fake = new AutoFake())
{
var dependency = A.Fake<IDependency>();
fake.Provide(dependency);
var sut = fake.Resolve<SystemUnderTest>();
}
// After (8.0.0)
var dependency = A.Fake<IDependency>();
using (var fake = new AutoFake(configureAction: cfg => cfg.RegisterInstance(dependency).As<IDependency>()))
{
var sut = fake.Resolve<SystemUnderTest>();
}For implementation registrations, register the type in configureAction and move any Autofac parameters onto the registration with WithParameter:
// Before (7.0.0): fake.Provide<IDependency, Dependency>();
// After (8.0.0):
using (var fake = new AutoFake(configureAction: cfg => cfg.RegisterType<Dependency>().As<IDependency>()))
{
var sut = fake.Resolve<SystemUnderTest>();
}Version 8.0.0 also removed the builder constructor parameter (the one that let you pass your own ContainerBuilder). It was redundant with configureAction, so move any registrations from your own builder into the configureAction callback.
See the Migrating from 7.0.0 documentation for the full details.
Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.