public void GetByIdShouldReturnCommentWithCorrectId()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnCommentWithCorrectId))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var commentsService = new CommentsService(context);
                var toAdd           = new CommentPostDTO()

                {
                    Important = true,
                    Text      = "A nice task...",
                };


                var current  = commentsService.Create(toAdd, null);
                var expected = commentsService.GetById(current.Id);



                Assert.IsNotNull(expected);
                Assert.AreEqual(expected.Text, current.Text);
                Assert.AreEqual(expected.Id, current.Id);
            }
        }
示例#2
0
        public void GetById_ShouldThrowKeyNotFoundException_IfInvalidIdIsGiven()
        {
            Mapper.Initialize(x => x.AddProfile <MapperConfiguration>());
            var repo     = new Mock <IRepository <Comment> >();
            var comments = GetTestData().AsQueryable();

            repo.Setup(r => r.All()).Returns(comments);
            var service = new CommentsService(repo.Object);

            //do
            var  expected = typeof(KeyNotFoundException);
            Type actual   = null;

            try
            {
                var result = service.GetById(3);
            }
            catch (KeyNotFoundException e)
            {
                actual = e.GetType();
            }

            //assert
            Assert.Equal(expected, actual);
        }
        public void GetCommentByIdTest()
        {
            var comment = _commentsService.GetById(1);

            _mock.Verify(m => m.GetById(1), Times.Once());
            Assert.AreEqual("Автор1", comment.Author);
            Assert.AreEqual("Коммент1", comment.Title);
            Assert.AreEqual("text", comment.Text);
        }
示例#4
0
        public void ThrowArgumentArgumentOutOfRangeExceptionWithProperMessageWhenIdParameterTIsNull(int id)
        {
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Comment> >();

            var commentsService = new CommentsService(mockedRepository.Object, mockedUnitOfWork.Object);

            Assert.That(() => commentsService.GetById(id),
                        Throws.TypeOf <ArgumentOutOfRangeException>().With.Message.Contains("id"));
        }
示例#5
0
 public ActionResult <Comment> Get(int id)
 {
     try
     {
         return(Ok(_cs.GetById(id)));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
示例#6
0
 [HttpGet("{commentssId}")] //Get By ID
 public ActionResult <Comment> GetById(string commentssId)
 {
     try
     {
         return(Ok(_service.GetById(commentssId)));
     }
     catch (System.Exception err)
     {
         return(BadRequest(err.Message));
     }
 }
 public ActionResult <Comment> GetById(int id)
 {
     try
     {
         Comment found = _service.GetById(id);
         return(Ok(found));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
        public async Task GetById_WithIncorrectId_ShouldReturnZeroComment(int id)
        {
            MapperInitializer.InitializeMapper();
            var dbContext = ApplicationDbContextCreatorInMemory.InitializeContext();

            await this.CreateTestComments(dbContext);

            var repository = new EfDeletableEntityRepository <Comment>(dbContext);
            var service    = new CommentsService(repository);

            Assert.Null(service.GetById <CommentEditModel>(id));
        }
示例#9
0
        public void CallCommentddRepositoryGetAllMethodWhenAuthorNameParameterIsValid()
        {
            var commentId = 1;

            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Comment> >();
            var authorsService   = new CommentsService(mockedRepository.Object, mockedUnitOfWork.Object);

            var result = authorsService.GetById(commentId);

            mockedRepository.Verify(x => x.GetById(commentId), Times.Once());
        }
示例#10
0
        public void ReturnNullIfCommentDoesNotExistWhenAuthorNameParameterIsValid()
        {
            var firstCommentId = 1;

            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Comment> >();
            var commentsService  = new CommentsService(mockedRepository.Object, mockedUnitOfWork.Object);

            mockedRepository.Setup(x => x.GetById(firstCommentId)).Returns((Comment)null);
            var result = commentsService.GetById(firstCommentId);

            Assert.IsNull(result);
        }
示例#11
0
        public async Task GetById_WithInvalidData_ShouldWorkCorrectly(int id)
        {
            var context = WilderExperienceContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            var repository = new EfDeletableEntityRepository <Comment>(context);
            var service    = new CommentsService(repository);

            var model = service.GetById <CommentViewModel>(id);

            Assert.Null(model);
        }
示例#12
0
        public async Task GetById_WithCorrectId_ShouldReturnCorrectComment()
        {
            MapperInitializer.InitializeMapper();
            var dbContext = ApplicationDbContextCreatorInMemory.InitializeContext();

            await this.CreateTestComments(dbContext);

            var repository = new EfDeletableEntityRepository <Comment>(dbContext);
            var service    = new CommentsService(repository);

            var result = service.GetById <CommentEditModel>(1);

            Assert.Equal(1, result.Id);
        }
示例#13
0
        public void GetById_ShouldReturnCommentViewModel_OfTheGivenCommentId()
        {
            Mapper.Initialize(x => x.AddProfile <MapperConfiguration>());
            var repo     = new Mock <IRepository <Comment> >();
            var comments = GetTestData().AsQueryable();

            repo.Setup(r => r.All()).Returns(comments);
            var service = new CommentsService(repo.Object);

            //do
            var result = service.GetById(1);

            //assert
            result.Should().NotBeNull().And.BeOfType <CommentViewModel>();
        }
示例#14
0
        public async Task GetById_ShouldWorkCorrectly()
        {
            var context = WilderExperienceContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            var repository = new EfDeletableEntityRepository <Comment>(context);
            var service    = new CommentsService(repository);

            var commentId = 1;
            var model     = service.GetById <CommentViewModel>(commentId);

            var expectedComment = repository.All().Where(x => x.Id == commentId).FirstOrDefault();

            Assert.Equal(expectedComment.Content, model.Content);
            Assert.Equal(expectedComment.UserId.ToString(), model.UserId.ToString());
            Assert.Equal(expectedComment.ExperienceId.ToString(), model.ExperienceId.ToString());
        }
示例#15
0
        public void ReturnCorrectAuthorWhenAuthorNameParameterIsValid()
        {
            var commentId = 1;

            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Comment> >();
            var commentsService  = new CommentsService(mockedRepository.Object, mockedUnitOfWork.Object);

            var mockedComment = new Mock <Comment>().Object;

            mockedComment.Id = commentId;

            mockedRepository.Setup(x => x.GetById(commentId)).Returns(mockedComment);

            var result = commentsService.GetById(commentId);

            Assert.AreSame(mockedComment, result);
        }
示例#16
0
        public async Task Edit_WithValidInput_ShouldChangeContent()
        {
            MapperInitializer.InitializeMapper();
            var dbContext = ApplicationDbContextCreatorInMemory.InitializeContext();

            await this.CreateTestComments(dbContext);

            var repository = new EfDeletableEntityRepository <Comment>(dbContext);
            var service    = new CommentsService(repository);

            var viewModel = new CommentEditModel()
            {
                Id      = 1,
                Content = "Edited",
            };

            await service.Edit(viewModel);

            var comment = service.GetById <CommentEditModel>(1);

            Assert.Equal("Edited", comment.Content);
        }
示例#17
0
 public ActionResult Edit(int id)
 {
     return(View(Cs.GetById(id)));
 }