英文:
How to Mock HttpContext to set Response.Cookie.Append() in UnitTest
问题
我需要模拟HttpContext并附加Cookie以在控制器中进行验证。
以下是我正在尝试的单元测试,但出现了空异常。
HomeController.ControllerContext = new ControllerContext();
HomeController.ControllerContext.HttpContext = new DefaultHttpContext();
HomeController.ControllerContext.HttpContext.Response.Cookies.Append("mycookie", cookieValue, cookieOptions);
这是我的控制器,我在其中检查Cookie是否为null。
public async Task<myDto> Identify() {
// 从客户端获取标识符
var identifier = identifiersService.GetIdentifier(HttpContext);
if (!await identifiersService.IsValidAsync(identifier)) {
if (HttpContext != null && HttpContext.Request.Cookies["mycookie"] != null) {
HttpContext.Response.Cookies.Delete("mycookie");
}
throw new MyException(Factory.INVALID);
}
}
有关如何从单元测试传递Cookie到控制器进行测试的任何建议都会受到欢迎。谢谢!
英文:
I need to mock HttpContext and append cookies to validate in controller.
Here is the unit Test I am trying but its failing with null exception.
HomeController.ControllerContext = new ControllerContext();
HomeController.ControllerContext.HttpContext = new DefaultHttpContext();
HomeController.ControllerContext.HttpContext.Response.Cookies.Append("mycookie", cookieValue, cookieOptions);
Here is the my controler where I am checking cooking null.
public async Task<myDto> Identify() {
// Get identifier from client
var identifier = identifiersService.GetIdentifier(HttpContext);
if (!await identifiersService.IsValidAsync(identifier)) {
if (HttpContext != null && HttpContext.Request.Cookies["mycookie"] != null) {
HttpContext.Response.Cookies.Delete("mycookie");
}
throw new MyException(Factory.INVALID);
}
Any suggestion on how I can pass cookie from unittest to Controller to test. Thank you
答案1
得分: 1
你在响应中设置了 cookie,然后在请求中检查它。
- 模拟请求中的 Cookies 集合
- 将模拟的
HttpContext
传递给ControllerContext
var cookiesMock = new Mock<IRequestCookieCollection>();
cookiesMock.SetupGet(c => c["mycookie"]).Returns(cookieValue);
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(ctx => ctx.Request.Cookies).Returns(cookiesMock.Object);
HomeController.ControllerContext = new ControllerContext()
{
HttpContext = httpContextMock.Object
};
更新 #1
如果 cookies 可用,我需要删除 cookie。我如何模拟
Response.Cookies.Delete()
?
与请求 cookies 的处理方式类似:
var responseCookiesMock = new Mock<IResponseCookies>();
responseCookiesMock.Setup(c => c.Delete("mycookie")).Verifiable();
httpContextMock.Setup(ctx => ctx.Response.Cookies).Returns(responseCookiesMock.Object);
因此,完整的代码如下:
const string CookieKey = "mycookie";
var requestCookiesMock = new Mock<IRequestCookieCollection>();
requestCookiesMock.SetupGet(c => c[CookieKey]).Returns(cookieValue);
var responseCookiesMock = new Mock<IResponseCookies>();
responseCookiesMock.Setup(c => c.Delete(CookieKey)).Verifiable();
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(ctx => ctx.Request.Cookies).Returns(requestCookiesMock.Object);
httpContextMock.Setup(ctx => ctx.Response.Cookies).Returns(responseCookiesMock.Object);
HomeController.ControllerContext = new ControllerContext()
{
HttpContext = httpContextMock.Object
};
英文:
You are setting the cookie on the response while you are checking it on the request.
- Mock the Cookies collection on the request
- Pass the mocked
HttpContext
to theControllerContext
var cookiesMock = new Mock<IRequestCookieCollection>();
cookiesMock.SetupGet(c => c["mycookie"]).Returns(cookieValue);
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(ctx => ctx.Request.Cookies).Returns(cookiesMock.Object);
HomeController.ControllerContext = new ControllerContext()
{
HttpContext = httpContextMock.Object
};
UPDATE #1
> If cookies is available then I need to delete the cookie. How I can I mock Response.Cookies.Delete()
?
Quite similarly what we did with the request cookies
var responseCookiesMock = new Mock<IResponseCookies>();
responseCookiesMock.Setup(c => c.Delete("mycookie"))
.Verifiable();
httpContextMock.Setup(ctx => ctx.Response.Cookies)
.Returns(responseCookiesMock.Object);
So, the full code would look like this
const string CookieKey = "mycookie";
var requestCookiesMock = new Mock<IRequestCookieCollection>();
requestCookiesMock.SetupGet(c => c[CookieKey]).Returns(cookieValue);
var responseCookiesMock = new Mock<IResponseCookies>();
responseCookiesMock.Setup(c => c.Delete(CookieKey)).Verifiable();
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(ctx => ctx.Request.Cookies)
.Returns(requestCookiesMock.Object);
httpContextMock.Setup(ctx => ctx.Response.Cookies)
.Returns(responseCookiesMock.Object);
HomeController.ControllerContext = new ControllerContext()
{
HttpContext = httpContextMock.Object
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论