public async Task CreateShortUrlReturnsCreatedResult() { // Mock setup var validator = new Mock <IDomainValidatorService>(); validator.Setup(v => v.IsValidDomain(Url)) .ReturnsAsync(true); var shortUrl = new Mock <IShortUrlService>(); shortUrl.Setup(s => s.AddUrl(Url)) .Returns(new ShortUrl { OriginalUrl = Url, ShortenedUrl = 0 }); // Run tests var controller = new UrlController(validator.Object, shortUrl.Object); var result = await controller.CreateShortUrl(Url); // Assertions var response = Assert.IsType <CreatedResult>(result); Assert.NotNull(response.Value); Assert.IsType <ShortUrl>(response.Value); Assert.Equal(Url, (response.Value as ShortUrl)?.OriginalUrl); Assert.Equal(0, ((ShortUrl)response.Value).ShortenedUrl); }
public async Task GetShortUrlReturnsRedirectResultWhenShortUrlIsValid() { // Mock setup var validator = new Mock <IDomainValidatorService>(); var shortUrl = new Mock <IShortUrlService>(); shortUrl.Setup(s => s.GetUrl(0)) .Returns(Url); // Run tests var controller = new UrlController(validator.Object, shortUrl.Object); await controller.CreateShortUrl(Url); var result = controller.GetShortUrl(0); // Assertions var response = Assert.IsType <RedirectResult>(result); Assert.Equal(Url, response.Url); }
public async Task CreateShortUrlReturnsBadRequestResultIfUrlInvalid() { // Mock setup var validator = new Mock <IDomainValidatorService>(); validator.Setup(v => v.IsValidDomain(InvalidUrl)) .ReturnsAsync(false); var shortUrl = new Mock <IShortUrlService>(); // Run tests var controller = new UrlController(validator.Object, shortUrl.Object); var result = await controller.CreateShortUrl(InvalidUrl); // Assertions var response = Assert.IsType <BadRequestObjectResult>(result); Assert.NotNull(response.Value); Assert.IsType <Dictionary <string, string> >(response.Value); Assert.Equal(1, (response.Value as IDictionary <string, string>)?.Keys.Count); Assert.True((response.Value as IDictionary <string, string>)?.ContainsKey("error")); Assert.Equal("invalid url", (response.Value as IDictionary <string, string>)?["error"]); }