-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
If your unit tests use
Mock<DefaultHttpContext>
, useMock<HttpContext>
instead.
After some research, it seems new DefaultHttpContext()
is a valid way way to mock HttpContext
in unit tests. Should this be added to the above-quoted line? Otherwise, it could be questioned whether newing a DefaultHttpContext()
is a Microsoft-accepted practice or Mock<HttpContext>
is the only recommended approach.
Supporting comments:
@JamesNK (dotnet/aspnetcore#6504 (comment)):
The difference is
HttpContext
requires you to mock out all the behavior that you need, whileDefaultHttpContext
lets you take the defaults and override only what you care about.
@davidfowl (dotnet/aspnetcore#6504 (comment)):
Most of the tests I see, new up the DefaultHttpContext (it was actually intentional to make it POCO like and newable).
@anurse (https://stackoverflow.com/questions/45959605/inspect-defaulthttpcontext-body-in-unit-test-situation#comment78887946_45960766)
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: df2066cb-244d-0b1b-cf46-428b198e8601
- Version Independent ID: e52631de-28de-77c2-082f-e79b89b17f04
- Content: Breaking changes, version 2.2 to 3.0 - .NET Core
- Content Source: docs/core/compatibility/2.2-3.0.md
- Product: dotnet-core
- GitHub Login: @gewarren
- Microsoft Alias: gewarren