views:

150

answers:

2

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);
+2  A: 

Pure guess here:

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

I tried below lines to mock HtmlEncode -

controller.Server.GetMockRepository().Stub(); controller.Server.Stub(x => x.HtmlEncode(criteria)).Return(criteria);

where controller is instance of Controller. But still below HtmlEncode code in action method is converting 'criteria' as null

'criteria' was set to some string value in unit tests which calls beloc action method.

public ActionResult ActionMethod(criteria) {

criteria = Server.HtmlEncode(criteria); }

Any idea how to encode HtmlEncode.

You should ask this as a new question.
Jason Watts