public async Task SaveAndUpdateStadium() { 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", CountryId = 1 }; await stadiumService.CreateAsync(stadiumViewModel); var updatedViewModel = new StadiumViewModel { Id = 1, Name = "Santiago Bernabeu", CountryId = 1 }; await stadiumService.UpdateAsync(updatedViewModel); var savedStadium = stadiumService.Get(1); Assert.Equal(1, savedStadium.Id); Assert.Equal("Santiago Bernabeu", savedStadium.Name); }
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); }