我正在尝试以下方法,但我得到:对象引用未设置为对象的实例。
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);
答案 0 :(得分:2)
纯粹猜测:
var mockServer = MockRepository.GenerateMock<HttpServerUtilityBase>();
mockServer.Expect(x => x.HtmlEncode("")).IgnoreArguments().Return("");