You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If your unit tests use Mock<DefaultHttpContext>, use Mock<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.
The difference is HttpContext requires you to mock out all the behavior that you need, while DefaultHttpContext lets you take the defaults and override only what you care about.
After some research, it seems new DefaultHttpContext() is a valid way way to mock HttpContext in unit tests.
Yep, it's a totally reasonable option. Since most of the properties are settable and easy to override it means you often don't even need a mock at all! I'm in favor of adding it as another alternative to that breaking change note.
https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.2-3.0#http-defaulthttpcontext-extensibility-removed
After some research, it seems
new DefaultHttpContext()
is a valid way way to mockHttpContext
in unit tests. Should this be added to the above-quoted line? Otherwise, it could be questioned whether newing aDefaultHttpContext()
is a Microsoft-accepted practice orMock<HttpContext>
is the only recommended approach.Supporting comments:
@JamesNK (dotnet/aspnetcore#6504 (comment)):
@davidfowl (dotnet/aspnetcore#6504 (comment)):
@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.
The text was updated successfully, but these errors were encountered: