public async Task Post_ReturnsBadRequest_OnUnhandledException()
        {
            _mockSubscriberService.Setup(a => a.Add(It.IsAny <Subscriber>())).ThrowsAsync(new Exception("A service error occurred")).Verifiable();

            var controller = new SubscriberController(_mockSubscriberService.Object, _logger.Object);
            var result     = await controller.Post(_fakeSubscriber);

            var badRequestResult = result as BadRequestResult;

            Assert.IsInstanceOf <ActionResult>(result);
            Assert.IsInstanceOf <BadRequestResult>(badRequestResult);
            _mockSubscriberService.VerifyAll();

            Assert.IsNotNull(badRequestResult);
            Assert.AreEqual((int)HttpStatusCode.BadRequest, badRequestResult.StatusCode);
        }
        public async Task Post_ReturnsBadRequest_OnFailedDbInsert()
        {
            _mockSubscriberService.Setup(a => a.Add(It.IsAny <Subscriber>())).Returns(Task.FromResult(new SubscriberResponse("error occurred") as ISubscriberResponse)).Verifiable();

            var controller = new SubscriberController(_mockSubscriberService.Object, _logger.Object);
            var result     = await controller.Post(_fakeSubscriber);

            var badRequestResult = result as BadRequestObjectResult;

            Assert.IsInstanceOf <ActionResult>(result);
            _mockSubscriberService.VerifyAll();

            Assert.IsInstanceOf(typeof(BadRequestObjectResult), badRequestResult);
            Assert.IsNotNull(badRequestResult);
            Assert.IsNotNull(badRequestResult.Value);
            Assert.AreEqual((int)HttpStatusCode.BadRequest, badRequestResult.StatusCode);
        }
        public async Task Post_ReturnsBadRequest_GivenInvalidModel()
        {
            _fakeSubscriber = new Subscriber();

            var controller = new SubscriberController(_mockSubscriberService.Object, _logger.Object);

            controller.ModelState.AddModelError("description", "Description is required");
            var result = await controller.Post(_fakeSubscriber);

            var badRequestResult = result as BadRequestObjectResult;

            Assert.IsInstanceOf <ActionResult>(result);
            Assert.IsInstanceOf(typeof(BadRequestObjectResult), badRequestResult);
            Assert.IsNotNull(badRequestResult);
            Assert.IsNotNull(badRequestResult.Value);
            Assert.AreEqual((int)HttpStatusCode.BadRequest, badRequestResult.StatusCode);
        }
        public async Task Post_ReturnsNewItemRoute()
        {
            _mockSubscriberService.Setup(a => a.Add(It.IsAny <Subscriber>())).Returns(Task.FromResult(new SubscriberResponse(_fakeSubscriber) as ISubscriberResponse)).Verifiable();

            var controller = new SubscriberController(_mockSubscriberService.Object, _logger.Object);
            var result     = await controller.Post(_fakeSubscriber);

            var createdAtActionResult = result as CreatedAtActionResult;

            Assert.IsInstanceOf <ActionResult>(result);
            Assert.IsInstanceOf <CreatedAtActionResult>(createdAtActionResult);
            Assert.AreEqual("Get", createdAtActionResult.ActionName);
            Assert.IsTrue(createdAtActionResult.RouteValues.ContainsKey("id"));
            Assert.AreEqual(createdAtActionResult.RouteValues["id"], _fakeSubscriber.Id);

            var returnValue = createdAtActionResult.Value as Subscriber;

            Assert.AreEqual(_fakeSubscriber.Id, returnValue.Id);
            _mockSubscriberService.VerifyAll();
        }