Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net c#APIController属性测试_C#_Asp.net Mvc_Testing - Fatal编程技术网

.net c#APIController属性测试

.net c#APIController属性测试,c#,asp.net-mvc,testing,C#,Asp.net Mvc,Testing,我想测试控制器上的属性是否正常工作。这些属性将允许和拒绝访问API方法中的资源。对于有效请求,它直接通过管道;对于无效请求,它停止管道并在到达方法之前抛出异常 我正在使用nUnit测试进行这些测试 因此,我可以测试控制器,但我需要测试属性是否对每个控制器都起作用。使用OWIN standalone server设置集成测试是最好的选择 然后,您可以在代码中模拟数据库/数据的依赖关系,并只测试attibute处理我不确定这是否对您有帮助。请试试邮递员工具。 如果您使用默认的System.Web.

我想测试控制器上的属性是否正常工作。这些属性将允许和拒绝访问API方法中的资源。对于有效请求,它直接通过管道;对于无效请求,它停止管道并在到达方法之前抛出异常

我正在使用nUnit测试进行这些测试


因此,我可以测试控制器,但我需要测试属性是否对每个控制器都起作用。

使用OWIN standalone server设置集成测试是最好的选择


然后,您可以在代码中模拟数据库/数据的依赖关系,并只测试attibute处理

我不确定这是否对您有帮助。请试试邮递员工具。
如果您使用默认的System.Web.Http.authorized属性,那么您实际上不需要测试它,因为它是。 如果您实现了自定义AuthorizeAttribute,那么您只需要测试您的授权逻辑(基本上测试actioncontext在不同测试场景中调用OnAuthorization方法后是否包含预期结果)。然后,您可以再次查看defaultauthorizeattribute测试,以了解如何执行该测试,例如(使用and):

使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Security.Principal;
使用System.Web.Http.Controller;
使用最小起订量;
公共类CustomAuthorizeAttributeTest
{
私有只读Mock_actionDescriptorMock=new Mock(){CallBase=true};
私有只读集合_allowAnonymousAttributeCollection=new集合(new AllowAnonymousAttribute[]{new AllowAnonymousAttribute()});
私有只读MockableAuthorizationAttribute_属性;
private readonly Mock_attributeMock=new Mock(){CallBase=true};
私有只读Mock _controllerDescriptorMock=new Mock(){CallBase=true};
专用只读HttpControllerContext\u controllerContext;
私有只读HttpActionContext\u actionContext;
private readonly Mock_principalMock=new Mock();
私有只读HttpRequestMessage _request=new HttpRequestMessage();
公共属性测试()
{
_attribute=\u attributeMock.Object;
_controllerContext=new Mock(){CallBase=true}.Object;
_controllerDescriptorMock.Setup(cd=>cd.GetCustomAttributes()).Returns(新集合(Enumerable.Empty().ToList());
_actionDescriptorMock.Setup(ad=>ad.GetCustomAttributes()).Returns(新集合(Enumerable.Empty().ToList());
_controllerContext.ControllerDescriptor=\u ControllerDescriptorLock.Object;
_controllerContext.Request=\u Request;
_actionContext=ContextUtil.CreateActionContext(_controllerContext,_actionDescriptorMock.Object);
_controllerContext.RequestContext.Principal=\u principalMock.Object;
}
[事实]
授权公开无效\u如果用户不是InUsersCollection()
{
_attribute.Users=“John”;
_principalMock.Setup(p=>p.Identity.IsAuthenticated)。返回(true)。可验证();
_principalMock.Setup(p=>p.Identity.Name).Returns(“Mary”).Verifiable();
_OnAuthorization(_actionContext);
AssertUnauthorizedRequestSet(_actionContext);
_principalMock.Verify();
}
私有静态void AssertUnauthorizedRequestSet(HttpActionContext actionContext)
{
Assert.NotNull(actionContext.Response);
Assert.Equal(HttpStatusCode.Unauthorized、actionContext.Response.StatusCode);
相同(actionContext.ControllerContext.Request、actionContext.Response.RequestMessage);
}
}
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web.Http.Controllers;
using Moq;

 public class CustomAuthorizeAttributeTest
{
        private readonly Mock<HttpActionDescriptor> _actionDescriptorMock = new Mock<HttpActionDescriptor>() { CallBase = true };
        private readonly Collection<AllowAnonymousAttribute> _allowAnonymousAttributeCollection = new Collection<AllowAnonymousAttribute>(new AllowAnonymousAttribute[] { new AllowAnonymousAttribute() });
        private readonly MockableAuthorizeAttribute _attribute;
        private readonly Mock<MockableAuthorizeAttribute> _attributeMock = new Mock<MockableAuthorizeAttribute>() { CallBase = true };
        private readonly Mock<HttpControllerDescriptor> _controllerDescriptorMock = new Mock<HttpControllerDescriptor>() { CallBase = true };
        private readonly HttpControllerContext _controllerContext;
        private readonly HttpActionContext _actionContext;
        private readonly Mock<IPrincipal> _principalMock = new Mock<IPrincipal>();
        private readonly HttpRequestMessage _request = new HttpRequestMessage();

        public AuthorizeAttributeTest()
        {
            _attribute = _attributeMock.Object;
            _controllerContext = new Mock<HttpControllerContext>() { CallBase = true }.Object;
            _controllerDescriptorMock.Setup(cd => cd.GetCustomAttributes<AllowAnonymousAttribute>()).Returns(new Collection<AllowAnonymousAttribute>(Enumerable.Empty<AllowAnonymousAttribute>().ToList()));
            _actionDescriptorMock.Setup(ad => ad.GetCustomAttributes<AllowAnonymousAttribute>()).Returns(new Collection<AllowAnonymousAttribute>(Enumerable.Empty<AllowAnonymousAttribute>().ToList()));
            _controllerContext.ControllerDescriptor = _controllerDescriptorMock.Object;
            _controllerContext.Request = _request;
            _actionContext = ContextUtil.CreateActionContext(_controllerContext, _actionDescriptorMock.Object);
            _controllerContext.RequestContext.Principal = _principalMock.Object;
        }

        [Fact]
        public void OnAuthorization_IfUserIsNotInUsersCollection()
        {
            _attribute.Users = "John";
            _principalMock.Setup(p => p.Identity.IsAuthenticated).Returns(true).Verifiable();
            _principalMock.Setup(p => p.Identity.Name).Returns("Mary").Verifiable();

            _attribute.OnAuthorization(_actionContext);

            AssertUnauthorizedRequestSet(_actionContext);
            _principalMock.Verify();
        }

        private static void AssertUnauthorizedRequestSet(HttpActionContext actionContext)
        {
            Assert.NotNull(actionContext.Response);
            Assert.Equal(HttpStatusCode.Unauthorized, actionContext.Response.StatusCode);
            Assert.Same(actionContext.ControllerContext.Request, actionContext.Response.RequestMessage);
        }
}