Ejemplo n.º 1
0
        public async Task DeleteAsync_DeleteForeignComment_Admin()
        {
            Mock <IUnitOfWork> UOWMock = new Mock <IUnitOfWork>();

            UOWMock.Setup(uow => uow.AdvertCommentRepository.GetAsync(1))
            .ReturnsAsync(new AdvertComment
            {
                Id     = 1,
                UserId = "user_1"
            });
            UOWMock.Setup(uow => uow.AdvertCommentRepository.Delete(It.IsAny <int>()));
            IAdvertCommentService commentService = new AdvertCommentService(_mapper, UOWMock.Object);

            await commentService.DeleteAsync(new DeleteCommentDto
            {
                Id       = 1,
                AdvertId = 1,
                UserId   = "admin"
            }, isAdmin : true);
        }
Ejemplo n.º 2
0
        public async Task DeleteAsync_DeleteForeignComment_NotAdmin()
        {
            Mock <IUnitOfWork> UOWMock = new Mock <IUnitOfWork>();

            UOWMock.Setup(uow => uow.AdvertCommentRepository.GetAsync(1))
            .ReturnsAsync(new AdvertComment
            {
                Id     = 1,
                UserId = "user_1"
            });
            UOWMock.Setup(uow => uow.AdvertCommentRepository.Delete(It.IsAny <int>()));
            IAdvertCommentService commentService = new AdvertCommentService(_mapper, UOWMock.Object);

            await Assert.ThrowsAsync <NotPermittedException>(async() =>
                                                             await commentService.DeleteAsync(new DeleteCommentDto
            {
                Id       = 1,
                AdvertId = 1,
                UserId   = "user_2"
            })
                                                             );
        }
Ejemplo n.º 3
0
        public async Task AddAsync_AddCommentToExistingAdvert()
        {
            //Создание Mock-объекта UnitOfWork
            Mock <IUnitOfWork> UOWMock = new Mock <IUnitOfWork>();

            //Настройка Mock объекта на вызов определённых методов
            //и возврат определённых значений из них
            UOWMock.Setup(uow => uow.AdvertRepository.GetAsync(1))
            .ReturnsAsync(new Advert {
                Id = 1
            });
            UOWMock.Setup(uow => uow.AdvertCommentRepository.Create(It.Is <AdvertComment>(ac => ac.Advert.Id == 1)));

            IAdvertCommentService testingCommentService = new AdvertCommentService(_mapper, UOWMock.Object);

            await testingCommentService.AddAsync(new CreateCommentDto
            {
                AdvertId = 1,
                Text     = "Текст",
                UserId   = "user_1",
                UserName = "******"
            });
        }