Moq Mocking and tracking Session values

后端 未结 4 1036
日久生厌
日久生厌 2021-01-13 05:18

I\'m having problems returning a Session value set from mocking using Moq. Using the following

public class TestHelpers
{
 public long sessionValue = -1;
 pu         


        
4条回答
  •  星月不相逢
    2021-01-13 06:18

    I just spent long time trying to figure out the easiest way to do it with moq, below is a copy past of my code that actually worked for me :

    var _adminctrl = new Moq.Mock(); //AdminController is my MVC controller
    
    var mock = new Mock();
    mock.Object.Controller = _adminctrl.Object;
    mock.Setup(p => p.HttpContext.Session["UserInfoKey"]).Returns(new ViewModel());
     //here is the catch, attaching the ControllerContext to your controller
    _adminctrl.Object.ControllerContext = mock.Object;
    

    hope this helps!

提交回复
热议问题