【问题标题】:ASP.NET MVC unit testing method that returns session返回会话的 ASP.NET MVC 单元测试方法
【发布时间】:2014-05-29 09:22:31
【问题描述】:

我正在使用 NUnit 和 Moq 进行单元测试。我尝试了这个示例来检查是否存在名为 Role 的会话对象。如果没有,则创建它,并返回 Role 类型的对象。

protected Role GetRole()
    {
        if (Session["Role"] == null)
        {
            Session["Role"] = new Role();
        }

        return Session["Role"] as Role;
    }

然后我在索引操作中使用它:

public ActionResult Index()
    {
        var roles = GetRole();
        roles.RoleName = "Test";
        return View();
    }

这是我的测试:

[Test]
    public void TestMethod1()
    {
        var contextMock = new Mock<ControllerContext>();
        var mockHttpContext = new Mock<HttpContextBase>();
        var session = new Mock<HttpSessionStateBase>();

        mockHttpContext.Setup(ctx => ctx.Session).Returns(session.Object);
        contextMock.Setup(ctx => ctx.HttpContext).Returns(mockHttpContext.Object);

        contextMock.Setup(p => p.HttpContext.Session["Role"]).Returns(new Role
                                                                      {
                                                                          RoleId = 1,
                                                                          RoleName = "Test"
                                                                      });
        var homeController = new HomeController();
        homeController.ControllerContext = contextMock.Object;
        var indexView = homeController.Index();
        Assert.IsNotNull(indexView);
    }

运行成功。但是当我检查代码覆盖率时,它给了我 Session["Role"] = new Role();部分未包含在测试代码中。于是我又做了一个测试。那里我没有设置会话变量角色:

[Test]
    public void TestMethod2()
    {
        var contextMock = new Mock<ControllerContext>();
        var mockHttpContext = new Mock<HttpContextBase>();
        var session = new Mock<HttpSessionStateBase>();

        mockHttpContext.Setup(ctx => ctx.Session).Returns(session.Object);
        contextMock.Setup(ctx => ctx.HttpContext).Returns(mockHttpContext.Object);
        var homeController = new HomeController();
        homeController.ControllerContext = contextMock.Object;
        var indexView = homeController.Index();

        Assert.IsNotNull(indexView);
        Assert.IsNull(homeController.ControllerContext.HttpContext.Session["Role"]);
    }

但它失败了 - 它给出了 System.NullReferenceException : Object reference not set to an instance of an object because the roles.RoleName = "Test";排。如何让它运行? 提前谢谢!

【问题讨论】:

    标签: c# asp.net-mvc unit-testing session


    【解决方案1】:

    您遇到的问题是,在第二个测试中,Session["Role"] 永远不会返回任何内容,因为它是一个设置为始终返回 null 的模拟对象。一种可能的解决方法是将您的 GetRole 函数更改为此并调整您的测试:

    protected Role GetRole()
    {
        var role = Session["Role"] as Role;
    
        if (role == null)
        {
            role = new Role();
            Session["Role"] = role;
        }
    
        return role;
    }
    

    【讨论】:

    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多