-1

I have an mvc application .I need to write unit test cases for that application. In controllers of this application i m reading an object from session

 objectclass Obj = (objectclass)Session["Object"];

I need to fake this reading of object using Shims/Stubs .

Ramveer Singh
  • 39
  • 1
  • 5

1 Answers1

1

You can create a method to access session data and mock the same like this. Create an interface.

interface ISessionHelper
{
    object GetSessionValue(string key);        

}

Now you can mock away the method call using any mocking framework.

Mock<ISessionHelper> sessionobj = new Mock<ISessionHelper>();
            sessionobj.Setup(a=>a.GetSessionValue(It.IsAny<string>())).Returns(new object());

Replace you seesion access code with

ISessionHelper obj = new SessionHelper();
obj.GetSessionValue("Object");
shbht_twr
  • 505
  • 3
  • 14