Beispiel #1
0
        public void Constructor_CreateInstance()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(Constructor_CreateInstance));
            var mockDateTimeProvider    = new Mock <IDateTimeProvider>();
            var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >();

            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object);
                Assert.IsNotNull(sut);
            }
        }
        public async Task ReturnNull_When_DtoIsNull()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnNull_When_DtoIsNull));
            var mockDateTimeProvider    = new Mock <IDateTimeProvider>();
            var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >();

            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object);
                var result = await sut.CreateCommentAsync(null);

                Assert.IsNull(result);
            }
        }
        public async Task Create_Comment_Correctly()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(Create_Comment_Correctly));
            var mockDateTimeProvider    = new Mock <IDateTimeProvider>();
            var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >();

            var user = new User {
                Id = 1
            };
            var bar = new Bar {
                Id = 1
            };

            var commentDto = new BarCommentDto
            {
                Id     = 1,
                UserId = 1,
                BarId  = 1,
                Text   = "TestComment",
            };

            mockBarCommentDtoMapper.Setup(x => x.MapDto(It.IsAny <BarComment>())).Returns(commentDto);
            using (var arrangeContext = new CocktailMagicianContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar);

                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();
            }
            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object);
                var result = await sut.CreateCommentAsync(commentDto);

                Assert.IsInstanceOfType(result, typeof(BarCommentDto));
                Assert.AreEqual(1, result.Id);
                Assert.AreEqual(1, result.BarId);
                Assert.AreEqual(1, result.UserId);
                Assert.AreEqual("TestComment", result.Text);
            }
        }
        public async Task ReturnNull_When_DtoParamsAreInvalid()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnNull_When_DtoParamsAreInvalid));
            var mockDateTimeProvider    = new Mock <IDateTimeProvider>();
            var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >();

            var user = new User {
                Id = 1
            };
            var bar = new Bar {
                Id = 1
            };

            var commentDto = new BarCommentDto
            {
                Id     = 1,
                UserId = 0,
                BarId  = 0,
                Text   = null,
            };

            using (var arrangeContext = new CocktailMagicianContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar);

                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();
            }
            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object);
                var result = await sut.CreateCommentAsync(null);

                Assert.IsNull(result);
            }
        }
        public async Task Return_CorrectModel_When_ParamsAreValid()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(Return_CorrectModel_When_ParamsAreValid));
            var mockDateTimeProvider    = new Mock <IDateTimeProvider>();
            var mockBarCommentDtoMapper = new Mock <IDtoMapper <BarComment, BarCommentDto> >();

            var user = new User {
                Id = 1
            };
            var bar = new Bar {
                Id = 1
            };

            var comment1 = new BarComment
            {
                Id     = 1,
                UserId = 1,
                BarId  = 1,
                Text   = "TestComment1",
            };
            var comment2 = new BarComment
            {
                Id     = 2,
                UserId = 1,
                BarId  = 1,
                Text   = "TestComment2",
            };
            var list = new List <BarCommentDto>
            {
                new BarCommentDto
                {
                    Id     = 1,
                    UserId = 1,
                    BarId  = 1,
                    Text   = "TestComment1",
                },
                new BarCommentDto
                {
                    Id     = 2,
                    UserId = 1,
                    BarId  = 1,
                    Text   = "TestComment2",
                }
            };

            mockBarCommentDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <BarComment> >())).Returns(list);

            using (var arrangeContext = new CocktailMagicianContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar);

                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.BarComments.AddAsync(comment1);

                await arrangeContext.BarComments.AddAsync(comment2);

                await arrangeContext.SaveChangesAsync();
            }

            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new BarCommentsService(assertContext, mockBarCommentDtoMapper.Object, mockDateTimeProvider.Object);
                var result = await sut.GetBarCommentsAsync(1);

                Assert.IsInstanceOfType(result, typeof(ICollection <BarCommentDto>));
                Assert.AreEqual(1, result.First().Id);
                Assert.AreEqual(1, result.First().UserId);
                Assert.AreEqual(1, result.First().BarId);
                Assert.AreEqual("TestComment1", result.First().Text);
                Assert.AreEqual(2, result.Last().Id);
                Assert.AreEqual(1, result.Last().UserId);
                Assert.AreEqual(1, result.Last().BarId);
                Assert.AreEqual("TestComment2", result.Last().Text);
            }
        }