public async Task EditAsyncThrowsWhenTheInputModelIsNull() { // Arrange var mapperMock = new Mock <IMapper>(); var repositoryMock = new Mock <IDeletableEntityRepository <Post> >(); var postsService = new PostsService(repositoryMock.Object, mapperMock.Object); // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { // Act await postsService.EditAsync("validId", null, new ImageInputModel()); }); }
public async Task <IActionResult> Edit(int id, PostCreateViewModel model) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (await _postsService.CanEditAsync(userId, id)) { if (ModelState.IsValid) { ViewBag.Id = id; await _postsService.EditAsync(id, model); return(RedirectToAction("Index", "Post")); } return(View(model)); } return(RedirectToAction("Index", "Post")); }
public async Task EditAsyncEditsThePropertiesAndSavesTheChanges( string userId, string title, string text, string categoryId, string newTitle, string newText, string newCategoryId, bool imageNull, string imageSource, string newImageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = false; var post = new Post() { ApplicationUserId = userId, Title = title, Text = text, CategoryId = categoryId, Image = new Image() { Source = imageSource, } }; var postsList = new List <Post>() { new Post(), post, new Post(), new Post(), new Post(), }; var repositoryMock = new Mock <IDeletableEntityRepository <Post> >(); repositoryMock.Setup(x => x.All()) .Returns(postsList.AsQueryable()); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var postsService = new PostsService(repositoryMock.Object, AutoMapperConfig.MapperInstance); var postEditModel = new PostInputModel() { Title = newTitle, Text = newText, CategoryId = newCategoryId, }; var imageEditModel = new ImageInputModel() { Source = newImageSource, }; if (imageNull) { imageEditModel = null; } // Act await postsService.EditAsync(post.Id, postEditModel, imageEditModel); // Assert Assert.True(saved); Assert.Equal(userId, post.ApplicationUserId); Assert.Equal(newTitle, post.Title); Assert.Equal(newText, post.Text); Assert.Equal(newCategoryId, post.CategoryId); var actualImage = post.Image; if (imageNull) { Assert.Equal(imageSource, actualImage.Source); } else { Assert.Equal(newImageSource, actualImage.Source); } }
public async Task EditMethodShouldWorkCorrectly(string title, string description, int categoryId) { var guid = Guid.NewGuid().ToString(); var options = new DbContextOptionsBuilder <ForumDbContext>() .UseInMemoryDatabase(guid) .Options; var db = new ForumDbContext(options); var dateTimeProvider = new Mock <IDateTimeProvider>(); dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27)); var post = new Post { Id = 1, Title = "Test title", Description = "Test description", Type = PostType.Discussion, CreatedOn = dateTimeProvider.Object.Now(), CategoryId = 1, AuthorId = guid, Tags = new List <PostTag> { new PostTag { TagId = 1 } } }; await db.Posts.AddAsync(post); await db.SaveChangesAsync(); var postsService = new PostsService(db, null, null, dateTimeProvider.Object); await postsService.EditAsync(1, title, description, categoryId, new[] { 1, 2, 3 }); var actual = await db.Posts.FirstOrDefaultAsync(); var expected = new Post { Id = 1, Title = title, Description = description, Type = PostType.Discussion, CreatedOn = dateTimeProvider.Object.Now(), ModifiedOn = dateTimeProvider.Object.Now(), CategoryId = categoryId, AuthorId = guid, Tags = new List <PostTag> { new PostTag { TagId = 1, PostId = 1, Post = post }, new PostTag { TagId = 2, PostId = 1, Post = post }, new PostTag { TagId = 3, PostId = 1, Post = post } } }; actual.Should().BeEquivalentTo(expected); }