gpt4 book ai didi

c# - 最小起订量模拟和跟踪 session 值

转载 作者:太空狗 更新时间:2023-10-29 22:13:15 25 4
gpt4 key购买 nike

我在使用最小起订量模拟返回 session 值集时遇到问题。使用以下内容

public class TestHelpers
{
public long sessionValue = -1;
public HttpContextBase FakeHttpContext()
{

var httpContext = new Mock<HttpContextBase>();
var session = new Mock<HttpSessionStateBase>();
httpContext.Setup(x => x.Session).Returns(session.Object);
httpContext.SetupGet(x => x.Session["id"]).Returns(sessionValue);
httpContext.SetupSet(x => x.Session["id"] = It.IsAny<long>())
.Callback((string name, object val) =>
{
sessionValue = (long)val;
});
}
}

当我尝试获取外部使用的值时

var th = new TestHelpers();
HttpContextBase httpContext = th.FakeHttpContext();

做一些设置 Session["id"]

的事情
var sessionid = httpContext.Session["id"];

sessionid 结果是-1。但是我可以使用

获取设置的 session 值
th.sessionValue

怎么了?难道我不能简单地通过 Moq 返回设置值吗?

最佳答案

我需要停止回答我自己的问题。事实证明我需要像这样再次模拟 Session["id"] ...

httpContext.SetupSet(x => x.Session["id"] = It.IsAny<long>())
.Callback((string name, object val) =>
{
sessionValue = (long)val;
httpContext.SetupGet(x => x.Session["id"]).Returns(sessionValue);
});

关于c# - 最小起订量模拟和跟踪 session 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1642631/

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