Exemplo n.º 1
0
        public async Task GetRightBarComment_Successfuly()
        {
            var testBarName  = "TestBarName";
            var testUserName = "******";
            var options      = Utils.GetOptions(nameof(GetRightBarComment_Successfuly));

            var testUser = new User()
            {
                Id       = Utils.MySampleGuid(),
                UserName = testUserName,
            };
            var testUser2 = new User()
            {
                Id       = Utils.MySampleGuid2(),
                UserName = testUserName,
            };
            var testBar = new Bar()
            {
                Id   = Utils.MySampleGuid3(),
                Name = testBarName,
            };
            var testComment = new BarUserComment()
            {
                UserId = testUser.Id,
                User   = testUser,
                BarId  = testBar.Id,
                Bar    = testBar,
                Text   = "Comment text here.",
            };
            var testComment2 = new BarUserComment()
            {
                UserId = testUser2.Id,
                User   = testUser2,
                BarId  = testBar.Id,
                Bar    = testBar,
                Text   = "Comment text here.2",
            };

            var mockMapper = new Mock <IBarUserCommentMapper>();

            mockMapper.Setup(x => x.MapDTOToEntity(It.IsAny <BarUserCommentDTO>()))
            .Returns((BarUserCommentDTO x) => new BarUserComment()
            {
                BarId  = x.BarId,
                UserId = x.UserId,
                Text   = x.Text
            });
            mockMapper.Setup(x => x.MapEntityToDTO(It.IsAny <BarUserComment>()))
            .Returns((BarUserComment x) => new BarUserCommentDTO()
            {
                BarId    = x.BarId,
                BarName  = x.Bar.Name,
                UserId   = x.UserId,
                UserName = x.User.UserName,
                Text     = x.Text
            });

            using (var arrangeContext = new BCcontext(options))
            {
                await arrangeContext.Users.AddAsync(testUser);

                await arrangeContext.Users.AddAsync(testUser2);

                await arrangeContext.Bars.AddAsync(testBar);

                await arrangeContext.BarComments.AddAsync(testComment);

                await arrangeContext.BarComments.AddAsync(testComment2);

                await arrangeContext.SaveChangesAsync();
            }

            //Act & Assert
            using (var context = new BCcontext(options))
            {
                var sut = new BarUserCommentsService(context, mockMapper.Object);

                Assert.IsNotNull(context.Bars
                                 .Include(x => x.Comments)
                                 .FirstOrDefault(x => x.Name == testBarName)
                                 .Comments.FirstOrDefault());
                Assert.AreEqual(2, context.BarComments.Count());
                //Act

                var result = await sut.GetAsync(testBar.Id, testUser.Id);

                //Assert
                Assert.IsNotNull(result);
                //Does not change other
                Assert.AreEqual(1, context.Bars.Count());
                Assert.AreEqual(2, context.Users.Count());
                Assert.AreEqual(2, context.BarComments.Count());
                //Correct comments
                Assert.AreEqual(testBar.Id, result.BarId);
                Assert.AreEqual(testUser.Id, result.UserId);
                Assert.AreEqual(testComment.Text, result.Text);
            }
        }
Exemplo n.º 2
0
        public async Task CreateComment_whenAlreadyCommented()
        {
            var testBarName  = "TestBarName";
            var testUserName = "******";
            var options      = Utils.GetOptions(nameof(CreateComment_whenAlreadyCommented));

            var testUser = new User()
            {
                Id       = Utils.MySampleGuid(),
                UserName = testUserName,
            };
            var testBar = new Bar()
            {
                Id   = Utils.MySampleGuid3(),
                Name = testBarName,
            };
            var testComment = new BarUserComment()
            {
                UserId = testUser.Id,
                User   = testUser,
                BarId  = testBar.Id,
                Bar    = testBar,
                Text   = "Comment text here.",
            };
            var dto = new BarUserCommentDTO()
            {
                UserId = testUser.Id,
                BarId  = testBar.Id,
                Text   = "New comment text here.",
            };

            var mockMapper = new Mock <IBarUserCommentMapper>();

            mockMapper.Setup(x => x.MapDTOToEntity(It.IsAny <BarUserCommentDTO>()))
            .Returns((BarUserCommentDTO x) => new BarUserComment()
            {
                BarId  = x.BarId,
                UserId = x.UserId,
                Text   = x.Text
            });
            mockMapper.Setup(x => x.MapEntityToDTO(It.IsAny <BarUserComment>()))
            .Returns((BarUserComment x) => new BarUserCommentDTO()
            {
                BarId    = x.BarId,
                BarName  = x.Bar.Name,
                UserId   = x.UserId,
                UserName = x.User.UserName,
                Text     = x.Text
            });

            using (var arrangeContext = new BCcontext(options))
            {
                await arrangeContext.Users.AddAsync(testUser);

                await arrangeContext.Bars.AddAsync(testBar);

                await arrangeContext.BarComments.AddAsync(testComment);

                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new BCcontext(options))
            {
                var sut = new BarUserCommentsService(context, mockMapper.Object);
                //Act
                var result = await sut.CreateAsync(dto);

                //Assert
                Assert.AreNotEqual(testBar.Id, result.BarId);
                Assert.IsNull(result.BarName);
                Assert.AreNotEqual(testUser.Id, result.UserId);
                Assert.IsNull(result.UserName);
                Assert.IsNull(result.Text);

                Assert.AreEqual(1, context.BarComments.Count());
                var bar = await context.Bars
                          .Include(x => x.Comments)
                          .FirstOrDefaultAsync(x => x.Name == testBarName);

                var comment = bar.Comments.FirstOrDefault();
                Assert.IsNotNull(comment);
                Assert.AreNotEqual(dto.Text, comment.Text);
            }
        }