Another day, another question. My service level has the following method
public MatchViewData CreateMatch(string user) { var matchViewData = !HasReachedMaxNumberOfMatchesLimit(user) ? CreateMatchAndAddToRepository(user) : MatchViewData.NewInstance(new Match(user)); matchViewData.LimitReached = HasReachedMaxNumberOfMatchesLimit(user); return matchViewData; }
The method calls this helper method to create a new matching object:
private MatchViewData CreateMatchAndAddToRepository(string user) { var match = new Match(user); MatchRepository.Add(match); return MatchViewData.NewInstance(match); }
The specified correspondence object is stored in the repository and id is set to some value> 0.
public void Add(Match match) { Check.Require(match != null); var numberOfMatchesBefore = Matches.Count; SetIdPerReflection(match, NextVal()); Matches.Add(match); Check.Ensure(numberOfMatchesBefore == Matches.Count - 1); }
The matchviewdata object copies some properties of the match object (including the identifier).
My unit test must make sure that the resulting viewdata object in the service has an identifier> 0. To archive this, I have to make fun of the repository and behavior of the add method. But the service method creates a new correspondence object each time it was called, and the add method in the repository updates the associated correspondence object (no return value is needed). I have no idea to solve this with moq.
This is my unit test so far:
[Test] public void ServiceCreateMatchReturnedMatchViewDataHasNonZeroId() { var match = TestUtils.FakePersistentMatch(User, 1); var repositoryMock = new Mock<IMatchRepository>(); repositoryMock.Setup( r => r.Add(It.IsAny<Match>())).Callback(() => match.Id = 1); var serviceFacade = new DefaultServiceFacade(repositoryMock.Object); var returnedMatch = serviceFacade.CreateMatch(User); Assert.That(returnedMatch.Id, Is.GreaterThan(0)); }
I tried some other options - nothing works.
source share