chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Can someone help....

We are using WCSF (Web Client Software Factory - MVP).

I am writing unit test and I need to mock IHttpSessionState value. How to do that?

Here is a partial code I am using to create MockRequest, MockContext etc.

Thanks in advance.....

In my presenter code some I am using something like:

string queryStringParam1 = _controller.Current.Context.QueryString["QueryStringParam1"];
string queryStringParam2 = _controller.Current.Context.QueryString["QueryStringParam2"];
string sessionParam = _controller.Context.Session["SessionParam1"];

----------------------

For this presenter I am writing Test and I am mocking as follows:

However I am not able to mock (or I don't know.....how to mock)
_controller.Context.Session["SessionParam1"]

Sample Code:

IHttpContextLocatorService _httpContextService = RecorderManager.CreateMockedObject<IHttpContextLocatorService>();

MockProcessController_controller = new MockProcessController(_httpContextService);

PresenterABC _presenter = new PresenterABC(_controller);

IHttpContext mockContext = (IHttpContext)MockManager.MockObject(typeof(IHttpContext)).Object;

MockObject<HttpRequest> mockRequest = MockManager.MockObject<HttpRequest>(Constructor.Mocked);

NameValueCollection mockQueryString = (NameValueCollection)RecorderManager.CreateMockedObject(typeof(NameValueCollection), Constructor.NotMocked);

mockQueryString.Add("QueryStringParam1", "Value1");
mockQueryString.Add("QueryStringParam2", "Value2");

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
recorder.ExpectAndReturn(_controller.Context, mockContext).Repeat(3);
recorder.ExpectAndReturn(mockContext.Request.QueryString, mockQueryString).Repeat(2);
recorder.ExpectAndReturn(mockRequest, mockContext.Request).Repeat(2);

recorder.ExpectAndReturn(_httpContextService.GetCurrentContext(), mockContext);
}
asked by tshah (1.2k points)

4 Answers

0 votes
Hi,

Thanks for reporting that. We'll take a look and get back to you.
answered by gilz (14.5k points)
0 votes
Hi,

First of all the solution. Since you are using Natural Mocks, you can easily write this test:
[TestMethod]
[VerifyMocks]
public void MockSession()
{

      IHttpContextLocatorService _httpContextService = RecorderManager.CreateMockedObject<IHttpContextLocatorService>(); 
      MockProcessController _controller = new MockProcessController(_httpContextService); 

      using (RecordExpectations recorder = RecorderManager.StartRecording()) 
      { 
          recorder.ExpectAndReturn(_controller.Context.Session["SessionParam1"], "expectedValue");
      }

      Assert.AreEqual("expectedValue", _controller.Context.Session["SessionParam1"]);
}


What you are trying to mock is called a "chain". It's very simple to mock using Natural Mocks.

Now, if I may, the code you posted seems a bit complex. You are mixing both reflective and Natural mocks unnecessarily. I would suggest the following:

1. Use Natural mocks when possible. It makes the code more readable.
2. When you use RecorderManager.CreateMockedObject, use it inside the recording block, unless the code outside uses the object.

Let me know if this works for you.

Thanks,
answered by gilz (14.5k points)
0 votes
Thanks for the reply. However suggestion didn't work.

I am getting following exception when I run the test:

Test method

TypeMock.VerifyException:
TypeMock Verification: Unexpected Call to Microsoft.Practices.CompositeWeb.Interfaces.IHttpContext.get_Session().
answered by tshah (1.2k points)
0 votes
Hi,

Since I can't reproduce it here, let's take it offline. I'll send instructions in a separate email.

Thanks,
answered by gilz (14.5k points)
...