Below is piece of the code of controller method(c#) :-
public ActionResult SelectProduct(string ProdName, int Block, int ProductAddressId = 0)
{
if (ProductAddressId == 0 && Block == 1 && System.Web.HttpContext.Current.Session["ReturnProductAddressID"] != null)
{
ProductAddressId = (int)System.Web.HttpContext.Current.Session["ReturnProductAddressID"];
}
//other stuffs………
}
Below is unit test method :-
[TestMethod]
public void SelectProduct_Condition1_Test()
{
//Arrange
var controller = new ProductController();
var prodName = string.Empty;
var block = 1;
var productAddressId = 0;
//section 1
/*var mockControllerContext = new Mock<ControllerContext>();
var mockSession = new Mock<HttpSessionStateBase>();
mockSession.SetupGet(s => s["ReturnProductAddressID"]).Returns("1");
mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object);*/
//section 2
/*int sessionValue = 1;
var mockControllerContext = new Mock<ControllerContext>();
var mockSession = new Mock<HttpSessionStateBase>();
mockSession.SetupSet(s => s["ReturnProductAddressID"] = It.IsAny<int>()).Callback((string name, object val) => sessionValue = (int)val);
mockSession.SetupGet(s => s["ReturnProductAddressID"]).Returns(() => sessionValue);
mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object);*/
//Act
var actual = controller.SelectProduct(prodName,block,productAddressId);
}
I want to ask how can i test or mock the session value up side on my action method(in if condition) ?
I have tried certain things in section 1 and section 2(commented section in unit test method above).But nothing is working for that.
So can anyone let me know how to do unit test for sessions ?
EDIT:
Nothing the above stuff working instead of below :-
System.Web.HttpContext.Current.Session["ReturnProductAddressID"] = "12";
means if i set the session value directly in unit test method.But i want to know will it be correct approach ?
To open this window, press Ctrl+Alt+T or choose ReSharper | Windows | Unit Test Sessions from the main menu. The window also opens automatically each time you run or debug unit tests from the current document or from the project/solution, create a new test session, or add tests to a test session.
You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.
You can use Mock. Here is how I have done before.
Download updated MoQ https://www.nuget.org/packages/moq
Moc session for the controller
var mockControllerContext = new Mock<ControllerContext>();
var mockSession = new Mock<HttpSessionStateBase>();
mockSession.SetupGet(s => s["ReturnProductAddressID"]).Returns("123"); //somevalue
mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object);
Register mockControllerContext for controller
var controller = new YourController();
controller.ControllerContext = mockControllerContext.Object;
Finally Act
var actual = controller.SelectProduct(YourModel);
So, your code would be something like this.
[TestMethod]
public void SelectProduct_Condition1_Test()
{
var prodName = string.Empty;
var block = 1;
var productAddressId = 0;
var mockControllerContext = new Mock<ControllerContext>();
var mockSession = new Mock<HttpSessionStateBase>();
mockSession.SetupGet(s => s["ReturnProductAddressID"]).Returns("123"); //somevalue
mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object);
var controller = new ProductController();
controller.ControllerContext = mockControllerContext.Object;
//Act
var actual = controller.SelectProduct(prodName, block, productAddressId);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With