ASP.NET MVC单元测试自定义AuthorizeAttribute [英] ASP.NET MVC unit testing custom AuthorizeAttribute

查看:128
本文介绍了ASP.NET MVC单元测试自定义AuthorizeAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个ASP.NET MVC 4项目(.NET Framework 4),我想知道如何正确地对自定义AuthorizeAttribute进行单元测试(我使用NUnit和Moq).

I'm working on an ASP.NET MVC 4 project (.NET framework 4) and I was wondering how to properly unit test a custom AuthorizeAttribute (I use NUnit and Moq).

我超越了2种方法:AuthorizeCore(HttpContextBase httpContext)HandleUnauthorizedRequest(AuthorizationContext filterContext).如您所见,这些方法分别期望HttpContextBaseAuthorizationContext,但是我不知道如何模拟这些.

I overrode 2 methods: AuthorizeCore(HttpContextBase httpContext) and HandleUnauthorizedRequest(AuthorizationContext filterContext). As you can see, these methods expect an HttpContextBase and AuthorizationContext respectively, but I don't know how to Mock these.

据我所知:

[Test]
public void HandleUnauthorizedRequest_UnexistingMaster_RedirectsToCommonNoMaster()
{
    // Arrange
    var httpContext = new Mock<HttpContextBase>();
    var winIdentity = new Mock<IIdentity>();
    winIdentity.Setup(i => i.IsAuthenticated).Returns(() => true);
    winIdentity.Setup(i => i.Name).Returns(() => "WHEEEE");
    httpContext.SetupGet(c => c.User).Returns(() => new ImdPrincipal(winIdentity.Object)); // This is my implementation of IIdentity
    var requestBase = new Mock<HttpRequestBase>();
    var headers = new NameValueCollection
        {
           {"Special-Header-Name", "false"}
        };
    requestBase.Setup(x => x.Headers).Returns(headers);
    requestBase.Setup(x => x.HttpMethod).Returns("GET");
    requestBase.Setup(x => x.Url).Returns(new Uri("http://localhost/"));
    requestBase.Setup(x => x.RawUrl).Returns("~/Maintenance/UnExistingMaster");
    requestBase.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(() => "~/Maintenance/UnExistingMaster");
    requestBase.Setup(x => x.IsAuthenticated).Returns(() => true);
    httpContext.Setup(x => x.Request).Returns(requestBase.Object);
    var controller = new Mock<ControllerBase>();
    var actionDescriptor = new Mock<ActionDescriptor>();
    var controllerContext = new ControllerContext(httpContext.Object, new RouteData(), controller.Object);

    // Act
    var masterAttr = new ImdMasterAuthorizeAttribute();
    var filterContext = new AuthorizationContext(controllerContext, actionDescriptor.Object);
    masterAttr.OnAuthorization(filterContext);

    // Assert
    Assert.AreEqual("", filterContext.HttpContext.Response);
}

在线:masterAttr.OnAuthorization(filterContext);抛出NullReferenceException.我认为它与我们尚未嘲弄的上下文中的值有关.

On the line: masterAttr.OnAuthorization(filterContext); a NullReferenceException is thrown. I presume it has something to do with a value in a context we haven't mocked yet.

非常感谢您的帮助.

谢谢.

此致, 亚尼克·库勒曼斯(Yanik Ceulemans)

Sincerely, Yanik Ceulemans

推荐答案

没有该属性的代码,只能猜测.但是要开始调查,您可以使用MockBehavior.Strict创建模拟.这样,如果在没有事先设置的情况下调用模拟中的方法或属性,Moq将引发异常.异常将具有方法/属性的名称:

Without the code for the attribute one can only guess. But to start investigating, you can create your mocks with MockBehavior.Strict. That way Moq will throw exception when a method or property on the mock are called without previous setup. The exception will have the name of the method/property:

var httpContext = new Mock<HttpContextBase>(MockBehavior.Strict);

这篇关于ASP.NET MVC单元测试自定义AuthorizeAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆