cara mengejek httpcontext abstrak menggunakan moq .net core
//Arrange
var request = new Mock<HttpRequest>();
request.Setup(x => x.Scheme).Returns("http");
request.Setup(x => x.Host).Returns(HostString.FromUriComponent("http://localhost:8080"));
request.Setup(x => x.PathBase).Returns(PathString.FromUriComponent("/api"));
var httpContext = Mock.Of<HttpContext>(_ =>
_.Request == request.Object
);
//Controller needs a controller context
var controllerContext = new ControllerContext() {
HttpContext = httpContext,
};
//assign context to controller
var controller = new SomeController(){
ControllerContext = controllerContext,
};
String expected = "expected value here";
//Act
String actual = await controller.SomeMethodPost();
//Assert
Assert.AreEqual(expected, actual);
//...
Sparkling Skunk