单元测试控制器模拟 ISession

问题描述 投票:0回答:3

我想对我的控制器进行单元测试,但我在从

HttpContext.Session
读取值时遇到问题。
我想从我的控制器中模拟这篇文章:
HttpContext.Session.Get<int>(Foo)

使用 Mock HttpContext 来对 .NET core MVC 控制器进行单元测试?如何在asp net core中模拟Session对象我能够得到以下解决方案:

我的测试:

public void GetFoos_AllGood_ReturnList()
{
  //Arrange
  Mock<ISession> sessionMock = new Mock<ISession>();
  var fooSessionValue = new byte[0];

  Web.Controllers.FooController fooController = new Web.Controllers.FooController();
  fooController.ControllerContext.HttpContext = new DefaultHttpContext();
  fooController.ControllerContext.HttpContext.Request.Headers["Foo"] = 0;

  Mock<IServiceCollection> mock = new Mock<IServiceCollection>();
  mock.Object.AddSession(); // Tried this, but failed

  //Setup
  sessionMock.Setup(_ => _.Set("Foo", It.IsAny<byte[]>())).Callback<string, byte[]>((k, v) => fooSessionValue = v);
  sessionMock.Setup(_ => _.TryGetValue("Foo", out fooSessionValue)).Returns(true);
  mockWebDataManager.Setup(b => b.GetFoos(It.IsAny<FooArgs>())).Returns(new FooResult() { Foos = new List<Foo>() });

  //Act
  var result = fooController.GetFoos() as JsonResult;

  //Assert
  Assert.NotNull(result);
  Assert.Equal(200, result.StatusCode);
}

我的控制器:

internal virtual DataArgs CreateArgs()
{
  return new FooArgs
  {
     HttpContext.Session.Get<int>(Foo) // I want to Mock this value    
  };
}

[HttpGet]
public IActionResult GetFoos()
{
  FooArgs args = CreateArgs();
  ...
}
c# asp.net-core mocking moq xunit
3个回答
6
投票

如果有人有一天遇到同样的问题,请看:

public void GetFoos_AllGood_ReturnList()
{
  //Arrange
  Mock<ISession> sessionMock = new Mock<ISession>();

  Web.Controllers.FooController fooController = new Web.Controllers.FooController();
  fooController.ControllerContext.HttpContext = new DefaultHttpContext();
  fooController.ControllerContext.HttpContext.Request.Headers["Foo"] = 0; 
  fooController.ControllerContext.HttpContext.Session = sessionMock.Object;

  //Act
  var result = fooController.GetFoos() as JsonResult;

  //Assert
  Assert.NotNull(result);
}

3
投票

感谢 Hypernate 的回答,但就我而言,我必须在单元测试中处理 GetInt32 和 SetInt32 扩展方法。根据下面的源代码是实现

public static void SetInt32(this ISession session, string key, int value)
        {
            var bytes = new byte[]
            {
                (byte)(value >> 24),
                (byte)(0xFF & (value >> 16)),
                (byte)(0xFF & (value >> 8)),
                (byte)(0xFF & value)
            };
            session.Set(key, bytes);
        }

public static int? GetInt32(this ISession session, string key)
        {
            var data = session.Get(key);
            if (data == null || data.Length < 4)
            {
                return null;
            }
            return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
        }

因此,按照答案提到的内容,我必须进行如下修改才能处理它。

 Mock<ISession> sessionMock = new Mock<ISession>();
        var key = "FY";
        int fy = 2020;
        var value =  new byte[]
        {
            (byte)(fy >> 24),
            (byte)(0xFF & (fy >> 16)),
            (byte)(0xFF & (fy >> 8)),
            (byte)(0xFF & fy)
        };

  sessionMock.Setup(_ => _.TryGetValue(key, out value)).Returns(true);

我在任何地方都找不到这个,所以在这里分享,以防它对遇到同样问题的人有帮助。


0
投票

根据 Hypenate 和 Emkay 的答案,对于 GetString 方法,我能够像这样模拟它:

MyController controller = new MyController();
Mock<ISession> sessionMock = new Mock<ISession>();
controller.ControllerContext.HttpContext = new DefaultHttpContext();
controller.ControllerContext.HttpContext.Session = sessionMock.Object;

var key = "FooKey";
var value = Encoding.UTF8.GetBytes("Value saved in the session string");
sessionMock.Setup(_ => _.TryGetValue(key, out value)).Returns(true);
© www.soinside.com 2019 - 2024. All rights reserved.