1

I am trying to test my controller but I keep getting the result of failed test and it says

Message: System.NullReferenceException : Object reference not set to an instance of an object.

How do I get the StatusCode of the IActionResult in this code? and compare it with the status code 400?

If I tried doing this "var actual = actionResult as BadRequestObjectResult; or BadRequestResult" it is null.

public class NotificationController_Tests
{
    public class Send
    {
        [Fact]
        public void Should_ReturnBadRequest_When_To_PropertyIsNull()
        {
            var mock_notifications = new Mock<INotifications>();
            var mock_logger = new Mock<ILogger<NotificationsController>>();

            var mock_notificationsController = new NotificationsController(mock_notifications.Object, mock_logger.Object);

            IActionResult actionResult = mock_notificationsController.Send(null) as IActionResult;
            var actual = actionResult as BadRequestObjectResult;

            Assert.NotNull(actionResult);
            Assert.Equal(400, actual.StatusCode);
        }
    }
}

}

Ivan Apungan
  • 532
  • 3
  • 15
  • I'm testing the return type, if it is badRequest or okResult, but it seems that when I try to convert it to a badRequestResult it says NULL. – Ivan Apungan Oct 17 '18 at 11:49

0 Answers0