public async Task AddAsyncShouldThrowNullExceptionForPost() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "AddAsync").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Comment>(dbContext); var service = new CommentsService(repository); await Assert.ThrowsAsync <ArgumentNullException>(() => service.AddAsync("content", new ApplicationUser(), null)); }
public async Task AddAsyncShouldWork(string content) { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "AddAsync").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Comment>(dbContext); var service = new CommentsService(repository); await service.AddAsync(content, new ApplicationUser(), new Post()); Assert.True(repository.All().Any(x => x.Content == content)); }
public async Task LikeShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "AddAsync").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Comment>(dbContext); var service = new CommentsService(repository); await service.AddAsync("content", new ApplicationUser(), new Post()); await service.Like(1, new ApplicationUser()); Assert.Equal(1, service.GetAll().FirstOrDefault(x => x.Content == "content").UserLikes.Count); }
public async Task AddAsyncThrowsWhenTheUserIdIsNull() { // Arrange var mapperMock = new Mock <IMapper>(); var repositoryMock = new Mock <IDeletableEntityRepository <Comment> >(); var commentsService = new CommentsService(repositoryMock.Object, mapperMock.Object); // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { // Act await commentsService.AddAsync(new CommentInputModel(), null); }); }
public async Task DeleteAsyncShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "DeleteAsync").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Comment>(dbContext); var service = new CommentsService(repository); await service.AddAsync("content", new ApplicationUser(), new Post()); Assert.True(repository.All().Any(x => x.Content == "content")); Assert.Single(service.GetAll()); await service.DeleteAsync(1); Assert.Empty(service.GetAll()); }
public async Task AddAsyncMapsTheInputModelAndAddsItToTheRepository(string postId, string text, string userId) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = false; var commentInput = new CommentInputModel() { PostId = postId, Text = text, }; var commentsList = new List <Comment>(); var repositoryMock = new Mock <IDeletableEntityRepository <Comment> >(); repositoryMock.Setup(x => x.AddAsync(It.IsAny <Comment>())) .Callback((Comment comment) => { commentsList.Add(comment); }); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var commentsService = new CommentsService(repositoryMock.Object, AutoMapperConfig.MapperInstance); // Act await commentsService.AddAsync(commentInput, userId); // Assert var actualComment = commentsList[0]; Assert.True(saved); Assert.Equal(postId, actualComment.PostId); Assert.Equal(text, actualComment.Text); Assert.Equal(userId, actualComment.ApplicationUserId); }