我如何模拟Server.HtmlEncode

时间:2010-01-20 16:55:25

标签: asp.net-mvc tdd mocking rhino-mocks

我正在尝试以下方法,但我得到:对象引用未设置为对象的实例。

 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);

马丁,

感谢。这足以让我指出这里提供的正确方向:

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 个答案:

答案 0 :(得分:2)

纯粹猜测:

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