Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding new DefaultHttpContext() to Mock<DefaultHttpContext> alternatives #17321

Closed
antmdvs opened this issue Mar 3, 2020 · 1 comment · Fixed by #17323
Closed

Consider adding new DefaultHttpContext() to Mock<DefaultHttpContext> alternatives #17321

antmdvs opened this issue Mar 3, 2020 · 1 comment · Fixed by #17323
Labels
doc-enhancement Improve the current content [org][type][category]

Comments

@antmdvs
Copy link
Contributor

antmdvs commented Mar 3, 2020

https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.2-3.0#http-defaulthttpcontext-extensibility-removed

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.

Supporting comments:

@JamesNK (dotnet/aspnetcore#6504 (comment)):

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.

@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.

@analogrelay
Copy link

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.

@Thraka Thraka added doc-enhancement Improve the current content [org][type][category] and removed ⌚ Not Triaged Not triaged labels Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org][type][category]
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants