Mocking之后单元测试会话仍然返回null

时间:2016-05-25 07:14:59

标签: asp.net-mvc unit-testing session mocking httpcontext

我正在尝试编写一个单元测试来测试我的控制器,它使用了session。我理解httpcontext仅从来自浏览器的请求生成的概念,并且因为调用控制器动作形式单元测试,当前上下文将为null。

我尝试根据本文的第一部分http://www.dontpaniclabs.com/blog/post/2011/03/22/testing-session-in-mvc-in-four-lines-of-code/解决此问题,但它仍然不适合我。请参阅下面的代码。

    [TestFixtureSetUp]
    public void Configure_AutoMapper()
    {
        _product = new ProductViewModel()
        {
            Id = Guid.NewGuid(),
            Category = Guid.Parse("72E42A11-DFA4-4AB9-A735-5CD5C18396D4"),
            Brand = Guid.Parse("96E68B67-60FB-4531-898E-815433B44D89"),
            Description = "From test",
            CostPrice = Parse("17.00"),
            SalePrice = Parse("25.00"),
            Quantity = 7,
            BusinessId = Guid.Parse("BDF9FB7F-DB40-4365-9872-AECD616ED415")
        };

        var context = new Mock<ControllerContext>();
        var session = new Mock<HttpSessionStateBase>();
        context.Setup(m => m.HttpContext.Session).Returns(session.Object);
        _productsController = new ProductsController {ControllerContext = context.Object};

        var userSessionInfo = new UserSessionViewModel()
        {
            FirstName = "Junior",
            LastName = "Labolz",
            BusinessId = Guid.Parse("BDF9FB7F-DB40-4365-9872-AECD616ED415")
        };

        SessionHelper.AddUserDetailsToSession(SessionHelper.UserDetails, userSessionInfo);
    }


    [Test]
    public void Display_Products_List_On_View_Products_Button_Click()
    {
        //Set Up

        //Act
        var viewResult = (ViewResult)_productsController.Index();

        //Assert
        Assert.That(string.Empty, Is.EqualTo(viewResult.ViewName));
    }


    public static void AddUserDetailsToSession(string sessionName, UserSessionViewModel userSessionDetails)
    {
        HttpContext.Current.Session.Add(sessionName, userSessionDetails);
    }

**根据理查德的回答,我将其改为**

        var context = new Mock<ControllerContext>();
        var session = new Mock<HttpSessionStateBase>();
        context.Setup(m => m.HttpContext.Session).Returns(session.Object);
        var userSessionInfo = new UserSessionViewModel()
        {
            FirstName = "Junior",
            LastName = "Labolz",
            BusinessId = Guid.Parse("BDF9FB7F-DB40-4365-9872-AECD616ED415")
        };
        context.Object.HttpContext.AddUserDetailsToSession(SessionHelper.UserDetails, userSessionInfo);

        _productsController = new ProductsController {ControllerContext = context.Object};

产品控制器构造函数

    public ProductsController()
    {
        _productService = new ProductServiceClient();
        _userSessionViewModel = (UserSessionViewModel)SessionHelper.GetSessionDetail(SessionHelper.UserDetails);
    }

获取会话方法

    public static object GetSessionDetail(string sessionName)
    {
        return HttpContext.Current.Session[sessionName];
    }

1 个答案:

答案 0 :(得分:0)

我可以看到你的代码包含一个直接引用HttpContext.Current的静态函数。如果你想进行单元测试,你不能这样做,你必须使用注入的HttpContextBase实例。

在您的单元测试中,这些实例将是您的模拟上下文。在生产中,您将设置容器以在请求HttpContextBase时注入HttpContext.Current。

我会将静态方法重写为扩展方法:

public static void AddUserDetailsToSession(this HttpContextBase context, 
    string sessionName, UserSessionViewModel userSessionDetails)
{
    context.Session.Add(sessionName, userSessionDetails);
}

然后从测试中调用它:

context.Object.AddUserDetailsToSession(SessionHelper.UserDetails, userSessionInfo);