public async Task EditMethodShouldChangeCategoryNameAndModifiedOn(string creationName, string editedName) { var options = new DbContextOptionsBuilder <ForumDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new ForumDbContext(options); var dateTimeProvider = new Mock <IDateTimeProvider>(); dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27)); await db.Categories.AddAsync(new Category { Name = creationName, CreatedOn = dateTimeProvider.Object.Now() }); await db.SaveChangesAsync(); var categoriesService = new CategoriesService(db, null, dateTimeProvider.Object); await categoriesService.EditAsync(1, editedName); var expected = new Category { Name = editedName, ModifiedOn = dateTimeProvider.Object.Now() }; var actual = await db.Categories.FirstOrDefaultAsync(); actual.Name.Should().BeSameAs(expected.Name); actual.ModifiedOn.Should().Be(expected.ModifiedOn); }
public async Task EditAsync_WithCorrectData_ShouldSuccessfullyEdit() { var testName = "TestName"; // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var categoryRepository = new EfDeletableEntityRepository <Category>(context); var categoriesService = new CategoriesService(categoryRepository); var inputModel = new CreateCategoryInputModel() { Name = testName, }; await categoriesService.CreateAsync(inputModel); var category = categoryRepository.All().FirstOrDefault(c => c.Name == testName); // Act var expectedCategoryName = "Edited_TestName"; var editInputModel = new CategoryInfoViewModel() { Id = category.Id, Name = expectedCategoryName, }; await categoriesService.EditAsync(editInputModel); var actualCategoryName = category.Name; // Assert category = await categoryRepository.GetByIdWithDeletedAsync(category.Id); Assert.Equal(expectedCategoryName, actualCategoryName); }
public async Task EditAsyncThrowsIfTheInputModelIsNull() { // Arrange var mapperMock = new Mock <IMapper>(); var repositoryMock = new Mock <IDeletableEntityRepository <Category> >(); var categoriesService = new CategoriesService(repositoryMock.Object, mapperMock.Object); // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { // Act await categoriesService.EditAsync("validId", null, new ImageInputModel()); }); }
public async Task EditAsyncCategoryNotValid() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Category>(new ApplicationDbContext(options.Options)); var categoryService = new CategoriesService(repository); AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly); repository.AddAsync(new Category() { Title = "test", Description = "test", ImageUrl = "test" }).GetAwaiter().GetResult(); repository.SaveChangesAsync().GetAwaiter().GetResult(); var category = categoryService.GetById(1); await categoryService.EditAsync(1, null, "asdasdas", "test1"); Assert.Equal("test", category.Title); }
public async Task EditAsync_WithIncorrectData_ShouldThrowArgumentNullException() { var incorrectId = Guid.NewGuid().ToString(); // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var categoryRepository = new EfDeletableEntityRepository <Category>(context); var categoriesService = new CategoriesService(categoryRepository); var editInputModel = new CategoryInfoViewModel() { Id = incorrectId, Name = "SomeTestName", }; // Act // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { await categoriesService.EditAsync(editInputModel); }); }
public async Task EditAsyncEditsThePropertiesAndSavesTheChanges( string name, string description, string newName, string newDescription, bool imageNull, string imageSource, string newImageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = false; var category = new Category() { Name = name, Description = description, Image = new Image() { Source = imageSource, }, }; var categoriesList = new List <Category>() { new Category(), new Category(), category, new Category(), new Category(), }; var repositoryMock = new Mock <IDeletableEntityRepository <Category> >(); repositoryMock.Setup(x => x.All()) .Returns(categoriesList.AsQueryable()); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var categoriesService = new CategoriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance); var categoryEditModel = new CategoryInputModel() { Name = newName, Description = newDescription, }; var imageEditModel = new ImageInputModel() { Source = newImageSource, }; if (imageNull) { imageEditModel = null; } // Act await categoriesService.EditAsync(category.Id, categoryEditModel, imageEditModel); // Assert Assert.True(saved); Assert.Equal(newName, category.Name); Assert.Equal(newDescription, category.Description); var actualImage = category.Image; if (imageNull) { Assert.Equal(imageSource, actualImage.Source); } else { Assert.Equal(newImageSource, actualImage.Source); } }