模拟深层方法
Mock deep level methods
我正在使用 Moq 模拟我的应用程序的一些对象,现在,我需要模拟这个调用:
accessor.HttpContext.Session.GetInt32("offset");
我试过这样做,但行不通:
var mock = new Mock<HttpContextAccessor>();
mock.Setup(x => x.HttpContext.Session.GetInt32("offset"))
.Returns(0);
它引发了这个错误:
System.NotSupportedException : Expression references a method that does not belong to the mocked object: x => x.HttpContext.Session.GetInt32("offset")
GetInt32 is an extension method that operates on the ISession
interface. You need to mock the ISession
interface so that the TryGetValue
方法 returns 预期值(GetInt32
调用 Get
调用 ISession
接口上的 TryGetValue
)然后return 来自 HttpContext.Session
的模拟
我正在使用 Moq 模拟我的应用程序的一些对象,现在,我需要模拟这个调用:
accessor.HttpContext.Session.GetInt32("offset");
我试过这样做,但行不通:
var mock = new Mock<HttpContextAccessor>();
mock.Setup(x => x.HttpContext.Session.GetInt32("offset"))
.Returns(0);
它引发了这个错误:
System.NotSupportedException : Expression references a method that does not belong to the mocked object: x => x.HttpContext.Session.GetInt32("offset")
GetInt32 is an extension method that operates on the ISession
interface. You need to mock the ISession
interface so that the TryGetValue
方法 returns 预期值(GetInt32
调用 Get
调用 ISession
接口上的 TryGetValue
)然后return 来自 HttpContext.Session