public async Task Create_Hero() { // Arrange int result; // Act var hero = new Hero { Age = 10, Name = "Izuku Midoriya", Nickname = "Deku", Individuality = "All for one", Team = "Team Midoriya", HeroType = HeroType.Student }; using (var context = CreateDbContext("Create_Hero")) { var repository = new HeroRepository(context); repository.Create(hero); result = await repository.SaveChangesAsync(); } // Assert result.Should().BeGreaterThan(0); result.Should().Be(1); // Simulate access from another context to verifiy that correct data was saved to database using (var context = CreateDbContext("Create_Hero")) { (await context.Heroes.CountAsync()).Should().Be(1); (await context.Heroes.FirstAsync()).Should().Be(hero); } }
public async Task Update_Hero() { // Arrange int result; Guid?id; using (var context = CreateDbContext("Update_Hero")) { var createdHero = new Hero() { Age = 10, Name = "Izuku Midoriya", Nickname = "Deku", Individuality = "All for one", Team = "Team Midoriya", HeroType = Domain.Entities.Enums.HeroType.Student }; context.Set <Hero>().Add(createdHero); context.Set <Hero>().Add(new Hero() { Name = "Another Hero", HeroType = Domain.Entities.Enums.HeroType.Vigilante, Age = 17 }); await context.SaveChangesAsync(); id = createdHero.Id; //receive autogenerated guid to get the entity later } // Act Hero updateHero; using (var context = CreateDbContext("Update_Hero")) { updateHero = await context.Set <Hero>().FirstOrDefaultAsync(x => x.Id == id); updateHero.Age = 15; updateHero.Individuality = "Blackwhip"; updateHero.Team = null; var repository = new HeroRepository(context); repository.Update(updateHero); result = await repository.SaveChangesAsync(); } // Assert result.Should().BeGreaterThan(0); result.Should().Be(1); // Simulate access from another context to verifiy that correct data was saved to database using (var context = CreateDbContext("Update_Hero")) { (await context.Heroes.FirstAsync(x => x.Id == updateHero.Id)).Should().Be(updateHero); } }
public async Task Delete_Hero() { // Arrange int result; Guid?id; using (var context = CreateDbContext("Delete_Hero")) { var createdHero = new Hero { Age = 10, Name = "Izuku Midoriya", Nickname = "Deku", Individuality = "All for one", Team = "Team Midoriya", HeroType = HeroType.Student }; context.Set <Hero>().Add(createdHero); context.Set <Hero>().Add(new Hero { Name = "Another Hero", HeroType = HeroType.Vigilante, Age = 17 }); await context.SaveChangesAsync(); id = createdHero.Id; //receive autogenerated guid to get the entity later } // Act using (var context = CreateDbContext("Delete_Hero")) { var repository = new HeroRepository(context); await repository.Delete(id.Value); result = await repository.SaveChangesAsync(); } // Assert result.Should().BeGreaterThan(0); result.Should().Be(1); // Simulate access from another context to verifiy that correct data was saved to database using (var context = CreateDbContext("Delete_Hero")) { (await context.Set <Hero>().FirstOrDefaultAsync(x => x.Id == id)).Should().BeNull(); (await context.Set <Hero>().ToListAsync()).Should().NotBeEmpty(); } }