public async Task SaveAndUpdateTeamPosition() { var clubsList = new List <Club> { new Club { Id = 1, Name = "Manchester United" }, new Club { Id = 2, Name = "Chelsea" } }; var seasonsList = new List <Season> { new Season { Id = 1, Name = "2020/21" } }; var teamPositionsList = new List <TeamPosition>(); var mockClubRepo = new Mock <IRepository <Club> >(); mockClubRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => clubsList.FirstOrDefault(c => c.Id == id)); var mockSeasonRepo = new Mock <IRepository <Season> >(); mockSeasonRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => seasonsList.FirstOrDefault(c => c.Id == id)); var mockTeamPositionRepo = new Mock <IRepository <TeamPosition> >(); mockTeamPositionRepo.Setup(r => r.All()).Returns(teamPositionsList.AsQueryable()); mockTeamPositionRepo.Setup(r => r.AddAsync(It.IsAny <TeamPosition>())).Callback <TeamPosition>(teamPosition => teamPositionsList.Add(new TeamPosition { Id = 1, Season = teamPosition.Season, Club = teamPosition.Club })); var teamPositionService = new TeamPositionService(mockTeamPositionRepo.Object, mockSeasonRepo.Object, mockClubRepo.Object); var teamPositionViewModel = new TeamPositionViewModel { SeasonId = 1, ClubId = 1 }; await teamPositionService.CreateAsync(teamPositionViewModel); var updatedViewModel = new TeamPositionViewModel { Id = 1, SeasonId = 1, ClubId = 2 }; await teamPositionService.UpdateAsync(updatedViewModel); var savedTeamPosition = teamPositionService.Get(1); Assert.Equal(1, savedTeamPosition.Id); }
public async Task SaveAndLoadTeamPosition() { var clubsList = new List <Club> { new Club { Id = 1, Name = "Manchester United" } }; var seasonsList = new List <Season> { new Season { Id = 1, Name = "2020/21" } }; var teamPositionsList = new List <TeamPosition>(); var mockClubRepo = new Mock <IRepository <Club> >(); mockClubRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => clubsList.FirstOrDefault(c => c.Id == id)); var mockSeasonRepo = new Mock <IRepository <Season> >(); mockSeasonRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => seasonsList.FirstOrDefault(c => c.Id == id)); var mockTeamPositionRepo = new Mock <IRepository <TeamPosition> >(); mockTeamPositionRepo.Setup(r => r.All()).Returns(teamPositionsList.AsQueryable()); mockTeamPositionRepo.Setup(r => r.AddAsync(It.IsAny <TeamPosition>())).Callback <TeamPosition>(teamPosition => teamPositionsList.Add(teamPosition)); mockTeamPositionRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => teamPositionsList.FirstOrDefault(c => c.Id == id)); var teamPositionService = new TeamPositionService(mockTeamPositionRepo.Object, mockSeasonRepo.Object, mockClubRepo.Object); var teamPositionViewModel = new TeamPositionViewModel { ClubId = 1, ClubName = "Manchester United", SeasonId = 1, SeasonName = "2020/21", Points = 50, Won = 15, Drawn = 5, Lost = 0, GoalsFor = 60, GoalsAgainst = 10 }; await teamPositionService.CreateAsync(teamPositionViewModel); var savedTeamPosition = teamPositionService.Get(10, false); var lastSavedTeamPosition = teamPositionService.GetAll().LastOrDefault(); Assert.Null(savedTeamPosition); Assert.Equal("2020/21", teamPositionViewModel.SeasonName); Assert.Equal("Manchester United", teamPositionViewModel.ClubName); Assert.Equal(50, teamPositionViewModel.Points); Assert.Equal(15, teamPositionViewModel.Won); Assert.Equal(5, teamPositionViewModel.Drawn); Assert.Equal(0, teamPositionViewModel.Lost); Assert.Equal(60, teamPositionViewModel.GoalsFor); Assert.Equal(10, teamPositionViewModel.GoalsAgainst); Assert.NotNull(lastSavedTeamPosition.Season); Assert.NotNull(lastSavedTeamPosition.Club); }