예제 #1
0
        public async Task Handle_WhenUrlCreationSucceeds_ReturnsSuccessfulResponse()
        {
            var expectedUrl = new ShortUrl
            {
                Adjective = new Adjective {
                    Value = "Adj"
                },
                Adverb = new Adverb {
                    Value = "Adv"
                },
                Noun = new Noun {
                    Value = "Nou"
                },
                BaseDomain  = "https://localhost/",
                DateCreated = DateTimeOffset.UtcNow,
                TargetUrl   = "https://localhost/"
            };

            _shortUrlRepositoryMock.Setup(m => m.CreateAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <PartOfSpeech>()))
            .ReturnsAsync(expectedUrl);

            var handler = new CreateUrlCommandHandler(_shortUrlRepositoryMock.Object);
            var actual  = await handler.Handle(new CreateUrlCommand { LanguageCode = "en" }, CancellationToken.None);

            Assert.True(actual.IsSuccessful);
            Assert.Equal(expectedUrl.DateCreated, actual.DateCreated);
            Assert.Equal(expectedUrl.TargetUrl, actual.TargetUrl);
            Assert.Equal("en", actual.Language);
            Assert.Equal($"https://localhost/AdvAdjNou", actual.Url);
        }
예제 #2
0
        public async Task Handle_WhenUrlCreationFails_ReturnsFailedResponse()
        {
            const string expectedError      = "Expected error message";
            const string expectedInnerError = "Expected error message inner";

            _shortUrlRepositoryMock.Setup(m => m.CreateAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <PartOfSpeech>()))
            .ThrowsAsync(new InvalidOperationException(expectedError, new Exception(expectedInnerError)));

            var handler = new CreateUrlCommandHandler(_shortUrlRepositoryMock.Object);
            var actual  = await handler.Handle(new CreateUrlCommand(), CancellationToken.None);

            Assert.False(actual.IsSuccessful);
            Assert.Equal(2, actual.Errors.Count);
            Assert.Equal(expectedError, actual.Errors[0].Message);
            Assert.Equal(expectedInnerError, actual.Errors[1].Message);
        }