unit-testing - 针对具有授权属性的 Controller 对匿名用户进行单元测试

标签 unit-testing asp.net-mvc-5

我正在尝试构建一个单元测试,以确保未经身份验证的用户无法访问 Controller 。当我运行测试时,发现用户已通过身份验证。我如何模拟事物,以便测试发现模拟的用户未经身份验证。

我正在使用身份为 2.0 的 mvc5

Controller

[Authorize]
public class ProfileController : Controller
{
    private ICompanyServiceLayer _service;

    public ProfileController(ICompanyServiceLayer service)
    {
        _service = service;
    }

    public ActionResult Index(int id)
    {
            /* cool stuff happens here */
        return View();
    }
}

测试

[Test]
public void Index_As_Annonymous_User()
{
    // arrange
    Mock<ICompanyServiceLayer> service = new Mock<ICompanyServiceLayer>();

    GenericIdentity id = new GenericIdentity("");
    Mock<IPrincipal> princ = new Mock<IPrincipal>();
    princ.Setup(x => x.Identity).Returns(id);


    Mock<HttpContextBase> contextBase = new Mock<HttpContextBase>();
    contextBase.Setup(x => x.User).Returns(princ.Object);

    Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
    controllerContext.Setup(x => x.HttpContext).Returns(contextBase.Object);

    // controller            
    ProfileController controller = new ProfileController(service.Object);
    controller.ControllerContext = controllerContext.Object;

    // act
    var result = controller.Index(1);

    // assert
    Assert.IsInstanceOf(typeof(HttpStatusCodeResult), result);
}

根据 blorkfish 建议进行更新

[Test]
public void Index_As_Annonymous_User()
{
    // arrange
    Mock<ICompanyServiceLayer> service = new Mock<ICompanyServiceLayer>();

    Mock<HttpRequestBase> request = new Mock<HttpRequestBase>();
    request.Setup(x => x.IsAuthenticated).Returns(false);

    Mock<HttpContextBase> contextBase = new Mock<HttpContextBase>();
    contextBase.Setup(x => x.Request).Returns(request.Object);

    // controller            
    ProfileController controller = new ProfileController(service.Object);
    controller.ControllerContext = new ControllerContext(contextBase.Object, new RouteData(), controller);

    // act
    var result = controller.Index(1);

    // assert
    Assert.IsInstanceOf(typeof(HttpStatusCodeResult), result);
}

最佳答案

使用 Moq,您需要模拟 HttpContextBase 并确保其 IsAuthenticated 属性返回 false。

   var mockHttpContext = new Mock<HttpContextBase>();

        mockHttpContext.SetupGet(c => c.User.Identity.IsAuthenticated).Returns(false);

        var mockControllerContext = new Mock<ControllerContext>();

        mockControllerContext.Setup(c => c.HttpContext).Returns(mockHttpContext.Object);

        controller.ControllerContext = mockControllerContext.Object;

然后,在 Controller 操作中运行以下命令应返回 false:

User.Identity.IsAuthenticated

关于unit-testing - 针对具有授权属性的 Controller 对匿名用户进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24112048/

相关文章:

unit-testing - 如何在 Typescript 中对私有(private)方法进行单元测试

ruby-on-rails - 如何断言 Rails 方法不会抛出 NoMethodError

c# - MVC Controller 找不到或无法访问服务器

asynchronous - 通用存储库 Get() IEnumerable 到异步

unit-testing - 编写单元测试需要多长时间?

unit-testing - 如何使自动工具测试读取文件?

javascript - 使用 jest 监视未导出的 node.js 函数未按预期工作

c# - 如何在 Razor 中单击按钮更改我的模型值?

c# - 检测是否手动调用了 AuthorizationAttribute

c# - Mvc 5 属性路由