public async Task SaveAndLoadStadium() { var countriesList = new List <Country> { new Country { Id = 1, Name = "Spain", Code = "SP" } }; var stadiumsList = new List <Stadium>(); var mockCountryRepo = new Mock <IRepository <Country> >(); mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id)); var mockStadiumRepo = new Mock <IRepository <Stadium> >(); mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable()); mockStadiumRepo.Setup(r => r.AddAsync(It.IsAny <Stadium>())).Callback <Stadium>(stadium => stadiumsList.Add(stadium)); mockStadiumRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => stadiumsList.FirstOrDefault(c => c.Id == id)); var stadiumService = new StadiumService(mockStadiumRepo.Object, mockCountryRepo.Object); var stadiumViewModel = new StadiumViewModel { Name = "Santiago Bernabeu", Capacity = 80000, FoundedOn = DateTime.Now, CountryId = 1, CountryName = "Spain" }; await stadiumService.CreateAsync(stadiumViewModel); var savedStadium = stadiumService.Get(10, false); var lastSavedStadium = stadiumService.GetAll().LastOrDefault(); Assert.Null(savedStadium); Assert.Equal("Santiago Bernabeu", lastSavedStadium.Name); Assert.Equal("Spain", stadiumViewModel.CountryName); Assert.Equal(80000, lastSavedStadium.Capacity); }
public async Task SaveAndDeleteStadium() { var countriesList = new List <Country> { new Country { Id = 1, Name = "Spain", Code = "SP" } }; var stadiumsList = new List <Stadium>(); var mockCountryRepo = new Mock <IRepository <Country> >(); mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id)); var mockStadiumRepo = new Mock <IRepository <Stadium> >(); mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable()); mockStadiumRepo.Setup(r => r.AddAsync(It.IsAny <Stadium>())).Callback <Stadium>(stadium => stadiumsList.Add(new Stadium { Id = 1, Name = stadium.Name, Capacity = stadium.Capacity, Country = stadium.Country })); mockStadiumRepo.Setup(r => r.Delete(It.IsAny <Stadium>())).Callback <Stadium>(stadium => stadiumsList.Remove(stadium)); var stadiumService = new StadiumService(mockStadiumRepo.Object, mockCountryRepo.Object); var stadiumViewModel = new StadiumViewModel { Name = "Santiago Bernabeu", CountryId = 1 }; await stadiumService.CreateAsync(stadiumViewModel); await stadiumService.DeleteAsync(1); Assert.Empty(stadiumService.GetAll(false)); }
public async Task SaveAndLoadStadiumsWithRelatedData() { var countriesList = new List <Country> { new Country { Id = 1, Name = "Spain", Code = "SP" } }; var stadiumsList = new List <Stadium>(); var mockCountryRepo = new Mock <IRepository <Country> >(); mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id)); var mockStadiumRepo = new Mock <IRepository <Stadium> >(); mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable()); mockStadiumRepo.Setup(r => r.AddAsync(It.IsAny <Stadium>())).Callback <Stadium>(stadium => stadiumsList.Add(new Stadium { Id = 1, Name = stadium.Name, Capacity = stadium.Capacity, Country = stadium.Country })); var stadiumService = new StadiumService(mockStadiumRepo.Object, mockCountryRepo.Object); var stadiumViewModel = new StadiumViewModel { Name = "Santiago Bernabeu", Capacity = 80000, FoundedOn = DateTime.Now, CountryId = 1 }; await stadiumService.CreateAsync(stadiumViewModel); var savedStadiums = stadiumService.GetAll(); Assert.True(savedStadiums.Count() == 1); }