Mock HttpContext for ASP.NET Core Unit Testing
What’s the first thing that comes to your mind when you realize your unit test needs to mock an HttpContext? 😭 It's not a difficult task, but may you agree that is boring.
I’ve often wondered if I could find a better way to write unit tests faster to decrease the resistance to writing them and even to improve their quality. So, I get down to business and wrote an HttContext Mock implementation that initializes out of the box all the properties and methods, without any setup.
HttpContextMoq
My proposal is the HttpContextMoq package available on nuget.org and source code on GitHub.
For example, If you need to mock a Referer header on your request, you need to write 5 lines of code, 6 if you consider the Mock<HttpContext>
creation.
With HttpContextMock you only need 1 line to the referer header setup.
What is the HttpContextMock class?
HttpContextMock is an implementation of AspNetCore.Http.HttpContext
that stores a Mock<HttpContext>
instance and works as a proxy for the real Mock.
The HttpContextMock
constructor initializes a Mock for Request, Response, Header and etc to work as a proxy as well.
For example, the HttpRequest
follows the same pattern and so one, and so one, until the last class pure method and properties of your request context.
So, when you create an HttpContextMock
instance all the setups are available and initialized.
How to use the HttpContextMoq?
Import the Nuget package HttpContextMoq:
Or, If you want to customize the implementation you can clone or/and copy the code to your project. See the source code here:
HttpContextMock Extensions
HttpContextMock has 5 extensions for the most uses cases of an ASP.NET Core unit testing:
- Request Url
HttpContextMock.SetupUrl()
- Request Headers
HttpContextMock.SetupRequestHeaders()
- Request Cookies
HttpContextMock.SetupRequestCookies()
- Session
HttpContextMock.SetupSession()
- Request Services
HttpContextMock.SetupRequestService()
In the image below, we can see a very useful mock of the URL https://localhost/mocks?assert=true
that will set up all the request and query properties and methods.
See the SetupUrl
source code here.
Session
extension, similarly to URL, set up the Session
and Features
properties of HttpContext
. See the example unit test below.
The Request.Headers
, Request.Cookies
and RequestServices
extensions try to step these properties with minimal effort also.
Use Cases
I recommend seeing the code Samples and some Unit Tests to check how you can set up your HttpContext
by Url, Session, Headers, Cookies, Query and etc.
- https://github.com/tiagodaraujo/httpcontextmoq/tree/master/tests/HttpContextMoq.Samples
- https://github.com/tiagodaraujo/httpcontextmoq/tree/master/tests/HttpContextMoq.Tests
Conclusion
Maybe this is not the best solution for you, and maybe not for everyone, but in my case, it saved me a lot of time along thousands of unit tests that I wrote with HttpContext.
If you have any suggestions, improvements, or something else, open an issue on Github or left a comment. I will be happy to know that this can help more people.
Tests safe.