public async Task GetAllStadiumsAsKeyValuePairs() { 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.All()).Returns(countriesList.AsQueryable()); 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 firstStadiumViewModel = new StadiumViewModel { Name = "Santiago Bernabeu", CountryId = 1, CountriesItems = new CountryService(mockCountryRepo.Object).GetAllAsKeyValuePairs() }; var secondStadiumViewModel = new StadiumViewModel { Name = "Camp Nou", CountryId = 1, CountriesItems = new CountryService(mockCountryRepo.Object).GetAllAsKeyValuePairs() }; await stadiumService.CreateAsync(firstStadiumViewModel); await stadiumService.CreateAsync(secondStadiumViewModel); var keyValuePairs = stadiumService.GetAllAsKeyValuePairs().ToList(); Assert.True(keyValuePairs.Count == 2); Assert.True(firstStadiumViewModel.CountriesItems.Count() == 1); Assert.True(secondStadiumViewModel.CountriesItems.Count() == 1); }