Beispiel #1
0
        public void ShouldThrowExceptionWhenNotFoundAEntityByCode()
        {
            var code = "Code1";

            var urlShortenDto = new ShortUrlDto
            {
                Code = "Code1",
                Url  = "www.example.com"
            };

            var urlShorten = new ShortUrl
            {
                Code = "Code1",
                Url  = "www.example.com"
            };

            creatorService.Setup(x => x.Execute(urlShortenDto))
            .Returns(urlShorten);

            var list = new List <ShortUrl>();

            var task = Task.FromResult <IList <ShortUrl> >(list);

            repository.Setup(x => x.ListAsync(It.IsAny <Expression <Func <ShortUrl, bool> > >()))
            .Returns(task);

            var service = new UrlShortenerService(repository.Object,
                                                  new GenerateCodeCommand(),
                                                  creatorService.Object,
                                                  new ShortenUrlUsageCommand(),
                                                  mapper.Object);

            Assert.ThrowsAsync <NotFoundException>(() => service.GetUrlByCode(code));
        }
Beispiel #2
0
        public void ShouldReturnShortUrlEntityFromCodeAndUpdate()
        {
            var code = "Code1";

            var urlShortenDto = new ShortUrlDto
            {
                Code = "Code1",
                Url  = "www.example.com"
            };

            var urlShorten = new ShortUrl
            {
                Code = "Code1",
                Url  = "www.example.com"
            };

            creatorService.Setup(x => x.Execute(urlShortenDto))
            .Returns(urlShorten);

            var list = new List <ShortUrl>
            {
                new ShortUrl {
                    Code = "Code2", Url = "www.example2.com"
                }
            };

            var task = Task.FromResult <IList <ShortUrl> >(list);

            repository.Setup(x => x.ListAsync(It.IsAny <Expression <Func <ShortUrl, bool> > >()))
            .Returns(task);

            var service = new UrlShortenerService(repository.Object,
                                                  new GenerateCodeCommand(),
                                                  creatorService.Object,
                                                  new ShortenUrlUsageCommand(),
                                                  mapper.Object);


            var result = service.GetUrlByCode(code).Result;

            repository.Verify(x => x.ListAsync(It.IsAny <Expression <Func <ShortUrl, bool> > >()), Times.Once);
            repository.Verify(x => x.Update(It.IsAny <ShortUrl>()), Times.Once);

            Assert.False(string.IsNullOrEmpty(result));
        }