如何在ASP.net核心单元测试项目中模拟Session变量? [英] How to Mock Session variables in ASP.net core unit testing project?
问题描述
如何在ASP.net核心单元测试项目中模拟会话变量?
1)我创建了一个会话的模拟对象.
模拟嘲笑HttpContext = new Mock();模拟嘲笑HttpContext = new Mock();模拟嘲笑Session = new Mock().As();
Mock mockHttpContext = new Mock(); Mock mockHttpContext = new Mock();Mock mockSession = new Mock().As();
2)设置GetString()方法
mockSession.Setup(s => s.GetString("ModuleId")).Returns("1");
mockSession.Setup(s => s.GetString("ModuleId")).Returns("1");
3)创建controllerContext并分配了模拟httpContext对象
controller.ControllerContext.HttpContext =模拟HttpContext.Object;
controller.ControllerContext.HttpContext = mockHttpContext.Object;
4)尝试从控制器读取.
HttpContext.Session.GetString("ModuleId")
HttpContext.Session.GetString("ModuleId")
而我得到的是"ModuleId"的空值.请帮助我模拟会话GetString()方法
Whereas I get a null value of "ModuleId". Please help me to mock session GetString() method
示例:
//Arrange
//Note: Mock session
Mock<HttpContext> mockHttpContext = new Mock<HttpContext>();
Mock<ITestSession> mockSession = new Mock<ISession>().As<ITestSession>();
//Cast list to IEnumerable
IEnumerable<string> sessionKeys = new string[] { };
//Convert to list.
List<string> listSessionKeys = sessionKeys.ToList();
listSessionKeys.Add("ModuleId");
sessionKeys = listSessionKeys;
mockSession.Setup(s => s.Keys).Returns(sessionKeys);
mockSession.Setup(s => s.Id).Returns("89eca97a-872a-4ba2-06fe-ba715c3f32be");
mockSession.Setup(s => s.IsAvailable).Returns(true);
mockHttpContext.Setup(s => s.Session).Returns(mockSession.Object);
mockSession.Setup(s => s.GetString("ModuleId")).Returns("1");
//Mock TempData
var tempDataMock = new Mock<ITempDataDictionary>();
//tempDataMock.Setup(s => s.Peek("ModuleId")).Returns("1");
//Mock service
Mock<ITempServices> mockITempServices= new Mock<ITempServices>();
mockITempServices.Setup(m => m.PostWebApiData(url)).Returns(Task.FromResult(response));
//Mock Management class method
Mock<ITestManagement> mockITestManagement = new Mock<ITestManagement>();
mockITestManagement .Setup(s => s.SetFollowUnfollow(url)).Returns(Task.FromResult(response));
//Call Controller method
TestController controller = new TestController (mockITestManagement .Object, appSettings);
controller.ControllerContext.HttpContext = mockHttpContext.Object;
controller.TempData = tempDataMock.Object;
//Act
string response = await controller.Follow("true");
// Assert
Assert.NotNull(response);
Assert.IsType<string>(response);
推荐答案
首先创建一个名为mockHttpSession的类,并从ISession继承.
First create class Named mockHttpSession and inherit from ISession.
public class MockHttpSession : ISession
{
Dictionary<string, object> sessionStorage = new Dictionary<string, object>();
public object this[string name]
{
get { return sessionStorage[name]; }
set { sessionStorage[name] = value; }
}
string ISession.Id
{
get
{
throw new NotImplementedException();
}
}
bool ISession.IsAvailable
{
get
{
throw new NotImplementedException();
}
}
IEnumerable<string> ISession.Keys
{
get { return sessionStorage.Keys; }
}
void ISession.Clear()
{
sessionStorage.Clear();
}
Task ISession.CommitAsync()
{
throw new NotImplementedException();
}
Task ISession.LoadAsync()
{
throw new NotImplementedException();
}
void ISession.Remove(string key)
{
sessionStorage.Remove(key);
}
void ISession.Set(string key, byte[] value)
{
sessionStorage[key] = value;
}
bool ISession.TryGetValue(string key, out byte[] value)
{
if (sessionStorage[key] != null)
{
value = Encoding.ASCII.GetBytes(sessionStorage[key].ToString());
return true;
}
else
{
value = null;
return false;
}
}
}
然后在实际控制器中使用此会话:
Then use this session in actual controller:
Mock<HttpContext> mockHttpContext = new Mock<HttpContext>();
MockHttpSession mockSession = new MockHttpSession();
mockSession["Key"] = Value;
mockHttpContext.Setup(s => s.Session).Returns(mockSession);
Controller controller=new Controller();
controller.ControllerContext.HttpContext = mockHttpContext.Object;
这篇关于如何在ASP.net核心单元测试项目中模拟Session变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!