gpt4 book ai didi

asp.net-mvc - 在模拟的 HttpContextBase 上设置属性

转载 作者:行者123 更新时间:2023-12-01 11:09:50 25 4
gpt4 key购买 nike

我正在开发一个 ASP.NET MVC 应用程序,并试图针对 Controller 操作编写一些单元测试,其中一些操作操作 HttpContext 上的属性,例如 Session、Request.Cookies、Response.Cookies 等。我在弄清楚如何“安排、行动、断言”时遇到了一些麻烦……我可以看到安排和断言……但是我在弄清楚如何对模拟 HttpContextBase 的属性“采取行动”时遇到了麻烦它的属性只有 setter/getter 。我无法从我的 Controller 操作中设置我的模拟上下文中的任何内容...所以它似乎不是很有用。我对模拟相当陌生,所以我确定我遗漏了一些东西,但对我来说似乎合乎逻辑的是我应该能够创建一个模拟对象,我可以在我测试 Controller 操作的上下文中使用它实际上可以设置属性值,然后断言它们仍然是我设置的值,或类似的东西。我错过了什么?

    public static HttpContextBase GetMockHttpContext()
{
var requestCookies = new Mock<HttpCookieCollection>();
var request = new Mock<HttpRequestBase>();
request.Setup(r => r.Cookies).Returns(requestCookies.Object);
request.Setup(r => r.Url).Returns(new Uri("http://example.org"));

var responseCookies = new Mock<HttpCookieCollection>();
var response = new Mock<HttpResponseBase>();
response.Setup(r => r.Cookies).Returns(responseCookies.Object);

var context = new Mock<HttpContextBase>();

context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(new Mock<HttpSessionStateBase>().Object);
context.Setup(ctx => ctx.Server).Returns(new Mock<HttpServerUtilityBase>().Object);
context.Setup(ctx => ctx.User).Returns(GetMockMembershipUser());
context.Setup(ctx => ctx.User.Identity).Returns(context.Object.User.Identity);
context.Setup(ctx => ctx.Response.Output).Returns(new StringWriter());

return context.Object;
}

最佳答案

不确定是否有人感兴趣,但我已经使用 Rhino Mocks(我选择的武器)将 Moq FakeHttpContext 翻译成一个。

public static HttpContextBase FakeHttpContext()
{
var httpContext = MockRepository.GenerateMock<HttpContextBase>();
var request = MockRepository.GenerateMock<HttpRequestBase>();
var response = MockRepository.GenerateMock<HttpResponseBase>();
var session = MockRepository.GenerateMock<HttpSessionStateBase>();
var server = MockRepository.GenerateMock<HttpServerUtilityBase>();
var cookies = new HttpCookieCollection();
response.Stub(r => r.Cookies).Return(cookies);
request.Stub(r => r.Cookies).Return(cookies);
request.Stub(r => r.Url).Return(new Uri("http://test.com"));

httpContext.Stub(x => x.Server).Return(server);
httpContext.Stub(x => x.Session).Return(session);
httpContext.Stub(x => x.Request).Return(request);
httpContext.Stub(x => x.Response).Return(response);
var writer = new StringWriter();
var wr = new SimpleWorkerRequest("", "", "", "", writer);
System.Web.HttpContext.Current = new System.Web.HttpContext(wr);
return httpContext;

}

在单元测试中的用法

_httpContext = FakeHttpContext();
var cookieManager = new CookieManager(_httpContext);
string username = cookieManager.GetUsername();
_httpContext.AssertWasCalled(hc => { var dummy = hc.Request; });

关于asp.net-mvc - 在模拟的 HttpContextBase 上设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1225511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com