vendredi 31 juillet 2015

Unit Test AuthorizeAttribute throws Object ref not set when attr [duplicate]

This question already has an answer here:

Hi I am trying to test my Custom AuthorizeAttribute.

It all works fine when the attribute returns false, but when the attribute returns true I get an : Object reference not set to an instance of an object.

Why on success would it throw this exception? I am assuming i am missing a setup but not sure what it is?

 [TestMethod]
        public void Given_AttributeReturnsTrue_Should_HttpUnauthorizedResult()
        {
            var context = new Mock<HttpContextBase>();

            context.Setup(c => c.Items).Returns(new Dictionary<object, object>());
            context.Setup(c => c.User.Identity.IsAuthenticated).Returns(true);
            context.SetupGet(x => x.Request).Returns(_requestMock.Object);

            var descriptorMock = new Mock<ActionDescriptor>();
            descriptorMock.Setup(a => a.ActionName).Returns("Index");
            var controllerDescriptor = new Mock<ControllerDescriptor>();

            descriptorMock.Setup(a => a.ControllerDescriptor).Returns(controllerDescriptor.Object);
            var controllerBase = new Mock<ControllerBase>();

            var controllerContext = new ControllerContext(_contextMock.Object, new RouteData(), controllerBase.Object);
            var authorisationContext = new AuthorizationContext(controllerContext, descriptorMock.Object)
            {
                HttpContext = controllerContext.HttpContext
            };

            SetupBusinessLogic(false);// this just setsup the logic to return false in the Attribute class when i step through attribute returns false

            attributeToTest.OnAuthorization(authorisationContext);

            authorisationContext.Result.ShouldBeOfType<HttpUnauthorizedResult>(); // test passes
        }

        public void Given_AttributeReturnsFalse_Should_DoSomething()
        {
            var context = new Mock<HttpContextBase>();

            context.Setup(c => c.Items).Returns(new Dictionary<object, object>());
            context.Setup(c => c.User.Identity.IsAuthenticated).Returns(true);
            context.SetupGet(x => x.Request).Returns(_requestMock.Object);

            var descriptorMock = new Mock<ActionDescriptor>();
            descriptorMock.Setup(a => a.ActionName).Returns("Index");
            var controllerDescriptor = new Mock<ControllerDescriptor>();

            descriptorMock.Setup(a => a.ControllerDescriptor).Returns(controllerDescriptor.Object);
            var controllerBase = new Mock<ControllerBase>();

            var controllerContext = new ControllerContext(_contextMock.Object, new RouteData(), controllerBase.Object);
            var authorisationContext = new AuthorizationContext(controllerContext, descriptorMock.Object)
            {
                HttpContext = controllerContext.HttpContext
            };

            SetupBusinessLogic(true);// this just setsup the logic to return true in the Attribute class when i step through attribute returns true

            attributeToTest.OnAuthorization(authorisationContext); // THROWS ERROR HERE OBJECT REF NOT SET TO REFERENCE OF OBJECT


        }

Aucun commentaire:

Enregistrer un commentaire