Document mixing real framework services with AutoMock (#49)#61
Merged
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #61 +/- ##
========================================
Coverage 90.99% 90.99%
========================================
Files 3 3
Lines 111 111
Branches 21 21
========================================
Hits 101 101
Misses 6 6
Partials 4 4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #49.
Background
In #49, constructing an EF Core
DbContextinside anAutoMockcontainer that also had logging wired up threw aNullReferenceExceptiondeep insideMicrosoft.Extensions.Logging.Root cause (not a defect to "fix" in code)
AutoMocksupplies a mock for any service it can't otherwise resolve, including the individual elements of an otherwise-empty collection dependency. SoIEnumerable<ILoggerProvider>— which is legitimately empty in normal DI — instead gets a single mock element injected. That loose mock'sCreateLoggerreturnsnull, andLoggerFactorythen dereferences the null.This single-element injection is intentional, long-standing behavior (it's what lets you test collection-style dependencies), and it's shared identically with the FakeItEasy integration. It is also non-recursive — it stops at the mock boundary and does not cascade into nested collections. So rather than change that behavior (a breaking change with real downsides), this documents the interaction and the workaround.
The test-ordering quirk in the original report is EF Core's static service-provider cache masking the problem when a "good" path runs first; it's outside our control.
Change
Adds
<remarks>to theAutoMockclass describing that adding real framework services into the container can yield null-returning mocks in framework-consumed collections, and how to avoid it (register the concrete services the framework consumes).Related
Companion documentation PR with the detailed guidance: autofac/Documentation#185