public async Task Post_ReturnsBadRequestObject_WhenUrlServiceThrowsError()
        {
            //arange
            var urlService = new Mock <IUrlService>();

            urlService.Setup(x => x.GetFinalLink(It.IsAny <HttpRequest>(), It.IsAny <string>()))
            .ReturnsAsync(() => throw new Exception("Error"));

            var logger            = new Mock <ILogger <ShortenedUrlsController> >();
            var controllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
            };
            var controller = new ShortenedUrlsController(logger.Object, urlService.Object)
            {
                ControllerContext = controllerContext
            };
            var vm = new CreateShortUrlViewModel()
            {
                Url = constUrl
            };

            //act
            var action = await controller.Post(vm);

            //assert
            var result = Assert.IsType <BadRequestObjectResult>(action);
            var value  = Assert.IsAssignableFrom <string>(result.Value);

            Assert.Equal("Error", value);
        }
        public async Task Post_CreatesShortUrl()
        {
            //arange
            var finalLink  = $"https://url.sh/{ranCod}";
            var request    = new Mock <HttpRequest>();
            var urlService = new Mock <IUrlService>();

            urlService.Setup(x => x.GetFinalLink(It.IsAny <HttpRequest>(), It.IsAny <string>()))
            .ReturnsAsync(() => finalLink);

            var logger            = new Mock <ILogger <ShortenedUrlsController> >();
            var controllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
            };
            var controller = new ShortenedUrlsController(logger.Object, urlService.Object)
            {
                ControllerContext = controllerContext
            };
            var vm = new CreateShortUrlViewModel()
            {
                Url = constUrl
            };

            //act
            var action = await controller.Post(vm);

            //assert
            var result   = Assert.IsType <CreatedResult>(action);
            var location = Assert.IsAssignableFrom <string>(result.Location);
            var value    = Assert.IsAssignableFrom <string>(result.Value);

            Assert.Equal(finalLink, location);
            Assert.Equal(finalLink, value);
        }
        public async Task Post_ReturnsBadRequest_WhenModelStateIsNotValid()
        {
            //arange
            var urlService = new Mock <IUrlService>();

            var logger     = new Mock <ILogger <ShortenedUrlsController> >();
            var controller = new ShortenedUrlsController(logger.Object, urlService.Object);
            var vm         = new CreateShortUrlViewModel()
            {
                Url = ""
            };

            controller.ModelState.AddModelError("Url", "Url is required");

            //act
            var action = await controller.Post(vm);

            //assert
            Assert.IsType <BadRequestResult>(action);
        }