FakeHttpContextAccessor Class
Definition
- Namespace
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http
- Assemblies
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Provides a unit test implementation of IHttpContextAccessor.
public class FakeHttpContextAccessor : IHttpContextAccessor
- Inheritance
-
FakeHttpContextAccessor
- Implements
Examples
The following example creates a FakeHttpContextAccessor instance for unit testing code that depends on IHttpContextAccessor. The fake initializes a DefaultHttpContext with FakeHttpRequestFeature and FakeHttpResponseFeature, providing a complete HTTP context without running a real server.
using System;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore.Http;
namespace WebFixtureTests;
public class HttpContextExample
{
public void Demonstrate()
{
var accessor = new FakeHttpContextAccessor();
Console.WriteLine(accessor.HttpContext.Request.Method);
}
}
Constructors
| Name | Description |
|---|---|
| FakeHttpContextAccessor(IServiceScopeFactory) | Initializes a new instance of the FakeHttpContextAccessor class. |
Properties
| Name | Description |
|---|---|
| HttpContext | Gets or sets the HTTP context. |