我有問題返回使用Moq嘲笑的會話值集。使用下面的Moq嘲笑和跟蹤會話值
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();
做一些東西,設置會話值[ 「ID」]
var sessionid = httpContext.Session["id"];
的SessionID真可謂爲-1。但我可以使用
th.sessionValue
怎麼了?我不能簡單地通過Moq返回設定值嗎?
這對我沒有用:@Konamimam似乎是正確的,.Callback不會被解僱。正確的方法似乎在這裏:http://stackoverflow.com/a/2921695/213609 – 2013-05-10 15:17:56