How can I mock Server.HtmlEncode

柔情痞子 提交于 2020-01-01 12:25:09

问题


I am trying the following, but I am getting : Object reference not set to an instance of an object.

 HttpContextBase mockContext = MockRepository.GenerateMock<HttpContextBase>();

 mockContext.Expect(c => c.Server.HtmlEncode("")).IgnoreArguments().Return("");
 mockContext.Expect(c => c.Server.HtmlDecode("")).Return("");

 controller.ControllerContext = 
    new ControllerContext(mockContext, new RouteData(), controller);

Matin,

Thanks. That was enough to point me in the right direction provided here:

var mockContext = MockRepository.GenerateMock<HttpContextBase>();       
var mockServer = MockRepository.GenerateMock<HttpServerUtilityBase>();

mockServer.Expect(s => s.HtmlEncode(""))
    .IgnoreArguments()
    .Repeat.AtLeastOnce()
    .Return("");

mockContext.Expect(c => c.Server)
    .IgnoreArguments()
    .Repeat.AtLeastOnce()
    .Return(mockServer);

controller.ControllerContext = 
    new ControllerContext(mockContext, new RouteData(), controller);

回答1:


Pure guess here:

var mockServer = MockRepository.GenerateMock<HttpServerUtilityBase>();
mockServer.Expect(x => x.HtmlEncode("")).IgnoreArguments().Return("");


来源:https://stackoverflow.com/questions/2103228/how-can-i-mock-server-htmlencode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!