public Match InsertMatch(Match match) { var existingMatch = unitOfWork.Repository<Match>() .Query(x => x.HomeTeamId == match.HomeTeamId && x.AwayTeamId == match.AwayTeamId && x.MatchDateTime == match.MatchDateTime) .Select().ToList() .FirstOrDefault(); if (existingMatch != null) { throw new DuplicateMatchException(existingMatch.Id); } unitOfWork.Repository<Match>().Insert(match); unitOfWork.SaveChanges(); return match; }
public void InsertMatch_Throws_DuplicateMatchException_If_Duplicate_Match_Already_Exists() { // Arrange var mockLeagueData = new List<League> { new League { Id = 1, IsDeleted = false } }; var mockTeamData = new List<Team> { new Team { Id = 1, LeagueId = 1 }, new Team { Id = 2, LeagueId = 1 } }; var mockMatchData = new List<Match> { new Match { Id = 1, HomeTeamId = 1, HomeScore = 1, AwayTeamId = 2, AwayScore = 2, MatchDateTime = new DateTime(2015, 10, 22, 09, 00, 00) } }; var duplicateMatchData = new Match { Id = 1, HomeTeamId = 1, HomeScore = 1, AwayTeamId = 2, AwayScore = 2, MatchDateTime = new DateTime(2015, 10, 22, 09, 00, 00) }; SetupMockRepository(mockLeagueData); SetupMockRepository(mockTeamData); SetupMockRepository(mockMatchData); // Act service.InsertMatch(duplicateMatchData); }
public void InsertMatch_Should_Insert_And_SaveChanges_Only_Once() { // Arrange var mockLeagueData = new List<League> { new League { Id = 1, IsDeleted = false } }; var mockTeamData = new List<Team> { new Team { Id = 1, LeagueId = 1 }, new Team { Id = 2, LeagueId = 1 } }; var match = new Match { HomeTeamId = 1, AwayTeamId = 2, HomeScore = 1, AwayScore = 0, MatchDateTime = new DateTime(2015, 10, 22, 09, 00, 00) }; // Act SetupMockRepository(mockLeagueData); SetupMockRepository(mockTeamData); SetupMockRepository<Match>(); // Need to set up the empty "table" of Match to insert into, else the insert will fail as there is no table to insert the row in. service.InsertMatch(match); // Insert (Add) following from the table creation above. // Assert MockSet<Match>().Verify(x => x.Add(match), Times.Once()); // Can use VerifyInsert<Match>(Times.Once()); // MockSet<Entities.Models.Match>().Verify(x => x.Add(It.IsAny<Match>()), Times.Once()); // Using the commented code line above verifys whether ANY Match object has been added to the context but NOT specifically the Match object called 'match' as instantiated above, hence why I have chosen to use the uncommented out line. MockUnitOfWork.Verify(x => x.SaveChanges(), Times.Once); // VerifySaveChanges(Times.Once()); // Commented out code line above is part of TestBase - same as above but extracted out for ease of re-use. }