最小起订量 - 惩戒MVC控制器的Response.Cookies.Clear() [英] MOQ - Mocking MVC Controller's Response.Cookies.Clear()
问题描述
我是新来的起订量,但使用它与NUnit的单元测试我。
I am new to MOQ, but am using it with NUnit for unit testing.
我有我的嘲笑控制器的所有部分,除了以下线,抛出一个'对象不设置到对象的实例错误信息。
I have all parts of my controller mocked, except the following line which throws an 'Object not set to an instance of an object' error message.
Response.Cookies.Clear();
我有以下的扩展方法来模拟它适用于一切我来翻过至今(非常感谢好心人对这个论坛)的控制器上下文。
I have the following extension method to mock the controller context which works for everything else I have come accross so far (very much thanks to the good people on this forum).
public static int SetUpForTest(this System.Web.Mvc.Controller ctl, string username, TestingUtility.Roles role)
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
request.SetupGet(x => x.ApplicationPath).Returns("/");
request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute));
request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection());
var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny<String>())).Returns((String url) => url);
// response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work
var context = new Mock<HttpContextBase>(MockBehavior.Strict);
context.SetupGet(x => x.Request).Returns(request.Object);
context.SetupGet(x => x.Response).Returns(response.Object);
context.SetupGet(x => x.Response.Cookies).Returns(new HttpCookieCollection()); // still can't call the Clear() method
//
// Mock the controller context (using the objects mocked above)
//
var moqCtx = new Mock<ControllerContext>(context.Object, new RouteData(), ctl);
moqCtx.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(username);
moqCtx.SetupGet(p => p.HttpContext.User.Identity.IsAuthenticated).Returns(true);
if (!string.IsNullOrEmpty(role.ToString()))
moqCtx.Setup(p => p.HttpContext.User.IsInRole(role.ToString())).Returns(true);
//
// Pass the mocked ControllerContext and create UrlHelper for the controller and return
//
ctl.ControllerContext = moqCtx.Object;
ctl.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);
return 1;
}
正如你可以看到上面我试图嘲弄的Cookies集合,这不利于的'得到'。
As you can see above I have tried to mock the 'get' of the cookies collection, which does not help.
另外,实际的Clear()方法不能嘲笑,因为它不是一个虚拟的方法。
Also, the actual Clear() method cannot be mocked as it is not a virtual method.
显然,我并不想测试的cookie被清除,我只是希望能够忽视它的测试。
Obviously I don't want to test that the cookies are being cleared, I just want to be able to ignore it in testing.
谢谢,
格雷格
推荐答案
这工作对我来说,当我做cookies.Clear()
This works for me when I do cookies.Clear()
var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
request.SetupGet(x => x.ApplicationPath).Returns("/");
request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute));
request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection());
var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny<String>())).Returns((String url) => url);
// response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work
var context = new Mock<HttpContextBase>(MockBehavior.Strict);
context.SetupGet(x => x.Request).Returns(request.Object);
context.SetupGet(x => x.Response).Returns(response.Object);
context.SetupGet(x => x.Response.Cookies).Returns(new HttpCookieCollection()); // still can't call the Clear() method
context.SetupGet(p => p.User.Identity.Name).Returns("blah");
context.SetupGet(p => p.User.Identity.IsAuthenticated).Returns(true);
var rc = new RequestContext(context.Object, new RouteData());
controller.ControllerContext = new ControllerContext(rc, controller);
这篇关于最小起订量 - 惩戒MVC控制器的Response.Cookies.Clear()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!