public async Task GetAllClubsAsKeyValuePairs() { var stadiumsList = new List <Stadium> { new Stadium { Id = 1, Name = "Old Trafford", Capacity = 76000 } }; var countriesList = new List <Country> { new Country { Id = 1, Name = "England", Code = "EN" } }; var clubsList = new List <Club>(); var mockStadiumRepo = new Mock <IRepository <Stadium> >(); mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable()); mockStadiumRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => stadiumsList.FirstOrDefault(c => c.Id == id)); 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 mockClubRepo = new Mock <IRepository <Club> >(); mockClubRepo.Setup(r => r.All()).Returns(clubsList.AsQueryable()); mockClubRepo.Setup(r => r.AddAsync(It.IsAny <Club>())).Callback <Club>(club => clubsList.Add(new Club { Id = 1, Name = club.Name, Country = club.Country, HomeStadium = club.HomeStadium })); var clubService = new ClubService(mockClubRepo.Object, mockCountryRepo.Object, mockStadiumRepo.Object); var firstClubViewModel = new ClubViewModel { Name = "Manchester United", CountryId = 1, HomeStadiumId = 1, StadiumsItems = new StadiumService( mockStadiumRepo.Object, mockCountryRepo.Object) .GetAllAsKeyValuePairs() }; var secondClubViewModel = new ClubViewModel { Name = "Newcastle United", CountryId = 1, HomeStadiumId = 1, CountriesItems = new CountryService(mockCountryRepo.Object) .GetAllAsKeyValuePairs() }; await clubService.CreateAsync(firstClubViewModel); await clubService.CreateAsync(secondClubViewModel); var keyValuePairs = clubService.GetAllAsKeyValuePairs().ToList(); Assert.True(keyValuePairs.Count == 2); Assert.True(firstClubViewModel.StadiumsItems.Count() == 1); Assert.True(secondClubViewModel.CountriesItems.Count() == 1); }