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); } }
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); }
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")); }
public ActionResult <Comment> Get(int id) { try { return(Ok(_cs.GetById(id))); } catch (Exception e) { return(BadRequest(e.Message)); } }
[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)); }
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()); }
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); }
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); }
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); }
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>(); }
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()); }
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); }
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); }
public ActionResult Edit(int id) { return(View(Cs.GetById(id))); }