Skip to content

Commit e303bf7

Browse files
committed
Document mixing real framework services with AutoMock (#49)
AutoMock injects a mock for any service it can't resolve, including the elements of an otherwise-empty collection dependency. A loose mock returns null for un-setup members, which can make a consuming framework (such as EF Core resolving logger providers) throw a NullReferenceException. Add remarks to the AutoMock class describing this and the workaround.
1 parent 595df74 commit e303bf7

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/Autofac.Extras.Moq/AutoMock.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ namespace Autofac.Extras.Moq;
99
/// <summary>
1010
/// Wrapper around <see cref="Autofac"/> and <see cref="Moq"/>.
1111
/// </summary>
12+
/// <remarks>
13+
/// <para>
14+
/// Adding real framework services (for example, Entity Framework Core or
15+
/// <c>Microsoft.Extensions.Logging</c>) into the <see cref="AutoMock"/> container can
16+
/// produce unexpected behavior. <see cref="AutoMock"/> automatically supplies a mock
17+
/// for any service it can't otherwise resolve, including the individual elements of an
18+
/// otherwise-empty collection dependency (such as <c>IEnumerable&lt;ILoggerProvider&gt;</c>).
19+
/// A loosely-mocked element returns <see langword="null" /> from members that aren't
20+
/// explicitly set up, which can cause the consuming framework to throw - for example a
21+
/// <see cref="NullReferenceException"/> deep inside the framework's own code.
22+
/// </para>
23+
/// <para>
24+
/// If you need real framework services in your test, register concrete implementations for
25+
/// the services that framework consumes (or avoid mixing real framework wiring into the
26+
/// auto-mocking container) so that <see cref="AutoMock"/> does not fill those slots with mocks.
27+
/// </para>
28+
/// </remarks>
1229
public class AutoMock : IDisposable
1330
{
1431
private readonly HashSet<Type> _createdServiceTypes = new();

0 commit comments

Comments
 (0)