For my program, it requires httpContext to be present, but, during MSTest, the values is set to "null".
Is there any way to provide the value to httpContext?
I was trying various methods, and the working method, without changes to the main controller was this:
public void Test_Method()
{
var httpContext = new DefaultHttpContext(); var stream = new
MemoryStream(Encoding.UTF8.GetBytes(""));
httpContext.Request.Body = stream; httpContext.Request.ContentLength =
stream.Length; ControllerName controller = new() {
ControllerContext = new ControllerContext()
{
HttpContext = httpContext
} };
}
Add the additional namespace packages:
using System.IO;
using Microsoft.AspNetCore.Http;
using System.Text;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.