C# 尝试使用webgrid对控制器进行单元测试时,HttpContext为null

C# 尝试使用webgrid对控制器进行单元测试时,HttpContext为null,c#,asp.net-mvc-3,unit-testing,C#,Asp.net Mvc 3,Unit Testing,我试图用webgrid对控制器进行单元测试 var grid = new WebGrid(ajaxUpdateContainerId: "container-grid",ajaxUpdateCallback: "setArrows", canSort: true); 我总是犯这个错误 System.ArgumentNullException: Value cannot be null. Parameter name: httpContext 这是我的测试方法 var mockCon

我试图用webgrid对控制器进行单元测试

var grid = new WebGrid(ajaxUpdateContainerId: "container-grid",ajaxUpdateCallback: "setArrows",  canSort: true);
我总是犯这个错误

System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext
这是我的测试方法

    var mockContext = CreateMockContext();
    UserController target = new UserController();
    target.ControllerContext = new ControllerContext();
    target.ControllerContext.HttpContext = mockContext.Http.Object;
    Nullable<int> page = new Nullable<int>();
    string sort = "CreatedDate";
    string sortdir = "ASC";
    ActionResult actual;
    actual = target.Payments(page, sort, sortdir);
    Assert.IsNotNull(actual);
var mockContext=CreateMockContext();
UserController目标=新的UserController();
target.ControllerContext=新的ControllerContext();
target.ControllerContext.HttpContext=mockContext.Http.Object;
可空页面=新的可空页面();
字符串sort=“CreatedDate”;
字符串sortdir=“ASC”;
行动结果实际;
实际=目标付款(页码、排序、排序);
Assert.IsNotNull(实际值);
这是我的CreateMockContext方法

public UnitTestBase CreateMockContext()
        {
            this.RoutingRequestContext = new Mock<RequestContext>(MockBehavior.Loose);
            this.ActionExecuting = new Mock<ActionExecutingContext>(MockBehavior.Loose);
            this.Http = new Mock<HttpContextBase>(MockBehavior.Loose);
            this.Server = new Mock<HttpServerUtilityBase>(MockBehavior.Loose);
            this.Response = new Mock<HttpResponseBase>(MockBehavior.Loose);
            this.Request = new Mock<HttpRequestBase>(MockBehavior.Loose);
            this.Session = new Mock<HttpSessionStateBase>(MockBehavior.Loose);
            this.Cookies = new HttpCookieCollection();

            this.RoutingRequestContext.SetupGet(c => c.HttpContext).Returns(this.Http.Object);
            this.ActionExecuting.SetupGet(c => c.HttpContext).Returns(this.Http.Object);
            this.Http.SetupGet(c => c.Request).Returns(this.Request.Object);
            this.Http.SetupGet(c => c.Response).Returns(this.Response.Object);
            this.Http.SetupGet(c => c.Server).Returns(this.Server.Object);
            this.Http.SetupGet(c => c.Session).Returns(this.Session.Object);
            this.Http.SetupGet(p => p.User.Identity.Name).Returns("admin");
            this.Http.SetupGet(p => p.Request.IsAuthenticated).Returns(true);
            this.Request.Setup(c => c.Cookies).Returns(Cookies);
            return this;
        }
public UnitTestBase CreateMockContext()
{
this.RoutingRequestContext=新模拟(MockBehavior.Loose);
this.ActionExecuting=newmock(MockBehavior.Loose);
this.Http=newmock(MockBehavior.Loose);
this.Server=newmock(MockBehavior.Loose);
this.Response=newmock(MockBehavior.Loose);
this.Request=newmock(MockBehavior.Loose);
this.Session=newmock(MockBehavior.Loose);
this.Cookies=new-HttpCookieCollection();
this.RoutingRequestContext.SetupGet(c=>c.HttpContext).Returns(this.Http.Object);
this.ActionExecuting.SetupGet(c=>c.HttpContext).Returns(this.Http.Object);
this.Http.SetupGet(c=>c.Request).Returns(this.Request.Object);
this.Http.SetupGet(c=>c.Response).Returns(this.Response.Object);
this.Http.SetupGet(c=>c.Server).Returns(this.Server.Object);
this.Http.SetupGet(c=>c.Session).Returns(this.Session.Object);
this.Http.SetupGet(p=>p.User.Identity.Name).Returns(“admin”);
this.Http.SetupGet(p=>p.Request.IsAuthenticated)。返回(true);
this.Request.Setup(c=>c.Cookies).Returns(Cookies);
归还这个;
}
我可以测试其他控制器。只有使用webgrid的控制器才会失败。
请帮助。

在控制器中实例化
WebGrid
有什么原因吗?这似乎是基于这样的原因,您可以将
WebGrid
实例化移动到控制器的视图中,并从其逻辑中删除该依赖关系。这肯定会使编写单元测试变得更容易。

我在做服务器端分页,需要将_db.Count()作为行计数传递,我不想通过ViewBag传递。我不熟悉
WebGrid
,但更好的方法可能是使用a传递计数。在我看来,管理网格元素是属于视图的问题,而不是控制器的责任。通过这种方式,控制器仍然专注于与模型交互并将适当的数据传递给视图。我这里也有同样的问题,但在我的例子中,我在ViewModel中实例化WebGrid。能够对我的模型的这一部分进行单元测试会很好,但看起来这是不可能的。