public async Task GetByIdAsyncShouldReturnNullIfCountryDoesntExists() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var addressesServiceMock = new Mock <IAddressesService>(); var citiesRepository = new EfDeletableEntityRepository <City>(dbContext); var repository = new EfDeletableEntityRepository <Country>(dbContext); var citiesServiceMock = new Mock <CitiesService>(citiesRepository, addressesServiceMock.Object); var service = new CountriesService(repository, citiesServiceMock.Object); var country1 = new Country() { Id = 1, }; var country2 = new Country() { Id = 2, }; var country3 = new Country() { Id = 3, }; dbContext.Add(country1); dbContext.Add(country2); dbContext.Add(country3); await dbContext.SaveChangesAsync(); var result = await service.GetByIdAsync <CountryServiceModel>(7); Assert.Null(result); }
public async Task GetByIdAsync_ShouldreturnCorrectResult() { MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var countriesRepository = new EfDeletableEntityRepository <Country>(context); var countriesService = new CountriesService(countriesRepository); var seeder = new DbContextTestsSeeder(); await seeder.SeedCountriesAsync(context); var expectedCountryId = await countriesRepository.All() .Where(c => c.Name == "Bulgaria") .Select(c => c.Id) .FirstOrDefaultAsync(); var actualCountry = await countriesService.GetByIdAsync <CountryServiceDetailsModel>(expectedCountryId); Assert.True(expectedCountryId == actualCountry.Id, ErrorMessage); }