// inFailoverMode passed as a Func as we only want to call it (expensive) when required private Func <int, Learner> GetStrategy(bool isLearnerArchived, Func <bool> inFailoverMode) { if (isLearnerArchived) { return((id) => _archiveService.GetLearner(id)); } else { ILearnerDataService dataService = inFailoverMode() ? _failoverDataService : _liveDataService; return((id) => DataOrArchive(dataService, id)); } }
public void GetLearnerTests( bool isLearnerArchived , bool inFailoverMode , bool learnerResponseArchived ) { var archiveLearner = new Learner(); A.CallTo(() => _archiveService.GetLearner(A <int> ._)).Returns(archiveLearner); var failoverLearner = new Learner(); var failoverLearnerResponse = new LearnerResponse { IsArchived = learnerResponseArchived, Learner = failoverLearner }; A.CallTo(() => _failoverDataService.GetLearner(A <int> ._)).Returns(failoverLearnerResponse); var liveLearner = new Learner(); var liveLearnerResponse = new LearnerResponse { IsArchived = learnerResponseArchived, Learner = liveLearner }; A.CallTo(() => _liveDataService.GetLearner(A <int> ._)).Returns(liveLearnerResponse); A.CallTo(() => _failoverService.InFailoverMode()).Returns(inFailoverMode); LearnerSource learnerSource = isLearnerArchived || learnerResponseArchived ? LearnerSource.Archive : inFailoverMode ? LearnerSource.Failover : LearnerSource.Live; bool archiveDataAccessed = isLearnerArchived || learnerResponseArchived; bool failoverDataAccessed = !isLearnerArchived && inFailoverMode; bool liveDataAccessed = !isLearnerArchived && !inFailoverMode; bool failoverServiceAccessed = !isLearnerArchived; var learner = _sut.GetLearner(1, isLearnerArchived); Learner expectedLearner; switch (learnerSource) { case LearnerSource.Archive: expectedLearner = archiveLearner; break; case LearnerSource.Failover: expectedLearner = failoverLearner; break; case LearnerSource.Live: expectedLearner = liveLearner; break; default: throw new ArgumentOutOfRangeException(nameof(learnerSource)); } Assert.AreSame(expectedLearner, learner); if (archiveDataAccessed) { A.CallTo(() => _archiveService.GetLearner(A <int> ._)).MustHaveHappenedOnceExactly(); } else { A.CallTo(() => _archiveService.GetLearner(A <int> ._)).MustNotHaveHappened(); } if (failoverDataAccessed) { A.CallTo(() => _failoverDataService.GetLearner(A <int> ._)).MustHaveHappenedOnceExactly(); } else { A.CallTo(() => _failoverDataService.GetLearner(A <int> ._)).MustNotHaveHappened(); } if (liveDataAccessed) { A.CallTo(() => _liveDataService.GetLearner(A <int> ._)).MustHaveHappenedOnceExactly(); } else { A.CallTo(() => _liveDataService.GetLearner(A <int> ._)).MustNotHaveHappened(); } if (failoverServiceAccessed) { A.CallTo(() => _failoverService.InFailoverMode()).MustHaveHappenedOnceExactly(); } else { A.CallTo(() => _failoverService.InFailoverMode()).MustNotHaveHappened(); } }