gpt4 book ai didi

c# - 如何对 session 值进行单元测试?

转载 作者:太空狗 更新时间:2023-10-29 20:04:40 25 4
gpt4 key购买 nike

下面是 Controller 方法的一段代码(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………
}

下面是单元测试方法:-

   [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) ?

我在第 1 节和第 2 节(上面单元测试方法中的注释部分)中尝试了某些事情。但是没有任何效果。

谁能告诉我如何对 session 进行单元测试?

编辑:

没有上面的东西代替下面的东西工作:-

System.Web.HttpContext.Current.Session["ReturnProductAddressID"] = "12";

意味着如果我直接在单元测试方法中设置 session 值。但我想知道它是否是正确的方法?

最佳答案

你可以使用模拟。这是我以前的做法。

下载更新的最小起订量 https://www.nuget.org/packages/moq

Controller 的 Moc session

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);

为 Controller 注册mockControllerContext

var controller = new YourController();
controller.ControllerContext = mockControllerContext.Object;

最后行动

var actual = controller.SelectProduct(YourModel);

所以,您的代码应该是这样的。

    [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);
}

关于c# - 如何对 session 值进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com