public void ReturnCorrectNumberOfRanks(int totalReviews, int expectedCount)
        {
            var reviews  = new List <BeerReview>();
            var strategy = new Mock <IRankCalculationStrategy>();

            for (var i = 0; i < totalReviews; i++)
            {
                var rv = this.Fixture.Create <BeerReview>();
                reviews.Add(rv);
            }

            var expectedGroups   = reviews.Where(r => !r.IsDeleted).GroupBy(r => r.Beer);
            var enumeratedGroups = expectedGroups as IGrouping <Beer, BeerReview>[] ?? expectedGroups.ToArray();

            for (var i = 0; i < enumeratedGroups.Length; i++)
            {
                var rank = new Mock <IBeerRank>();
                rank.SetupGet(r => r.CompositeScore)
                .Returns(i);
                strategy.Setup(s => s.GetBeerRank(enumeratedGroups[i], enumeratedGroups[i].Key))
                .Returns(rank.Object);
            }

            var repository = new Mock <IRepository <BeerReview> >();

            repository.SetupGet(r => r.All)
            .Returns(reviews.AsQueryable());

            var topBeersService = new TopBeersService(repository.Object, strategy.Object);

            var result = topBeersService.GetTopBeers(expectedCount);

            Assert.GreaterOrEqual(expectedCount, result.Count());
        }
        public void CallGetRankForEachReviewGroupWithSameBeer()
        {
            // Arrange
            var totalReviews = 15;
            var reviews      = new List <BeerReview>();
            var strategy     = new Mock <IRankCalculationStrategy>();

            for (var i = 0; i < totalReviews; i++)
            {
                var rv = this.Fixture.Create <BeerReview>();
                reviews.Add(rv);
            }

            var expectedGroups   = reviews.Where(r => !r.IsDeleted).GroupBy(r => r.Beer);
            var enumeratedGroups = expectedGroups as IGrouping <Beer, BeerReview>[] ?? expectedGroups.ToArray();

            for (var i = 0; i < enumeratedGroups.Length; i++)
            {
                var rank = new Mock <IBeerRank>();
                rank.SetupGet(r => r.CompositeScore)
                .Returns(i);
                strategy.Setup(s => s.GetBeerRank(enumeratedGroups[i], enumeratedGroups[i].Key))
                .Returns(rank.Object);
            }

            var repository = new Mock <IEfRepository <BeerReview> >();

            repository.SetupGet(r => r.All)
            .Returns(reviews.AsQueryable());

            var topBeersService = new TopBeersService(repository.Object, strategy.Object);

            // Act
            topBeersService.GetTopBeers(10);

            // Assert
            foreach (var expectedGroup in enumeratedGroups)
            {
                strategy.Verify(s => s.GetBeerRank(expectedGroup, expectedGroup.Key), Times.Once);
            }
        }
        public void ReturnRanksOrderedByDescendingCompositeScore(int totalReviews, int expectedCount)
        {
            // Arrange
            var reviews  = new List <BeerReview>();
            var strategy = new Mock <IRankCalculationStrategy>();

            for (var i = 0; i < totalReviews; i++)
            {
                var rv = this.Fixture.Create <BeerReview>();
                reviews.Add(rv);
            }

            var expectedGroups   = reviews.Where(r => !r.IsDeleted).GroupBy(r => r.Beer);
            var enumeratedGroups = expectedGroups as IGrouping <Beer, BeerReview>[] ?? expectedGroups.ToArray();

            for (var i = 0; i < enumeratedGroups.Length; i++)
            {
                var rank = new Mock <IBeerRank>();
                rank.SetupGet(r => r.CompositeScore)
                .Returns(i);
                strategy.Setup(s => s.GetBeerRank(enumeratedGroups[i], enumeratedGroups[i].Key))
                .Returns(rank.Object);
            }

            var repository = new Mock <IEfRepository <BeerReview> >();

            repository.SetupGet(r => r.All)
            .Returns(reviews.AsQueryable());

            var topBeersService = new TopBeersService(repository.Object, strategy.Object);

            // Act
            var result = topBeersService.GetTopBeers(expectedCount);

            // Assert
            var comparer = Comparer <IBeerRank> .Create((a, b) => b.CompositeScore.CompareTo(a.CompositeScore));

            CollectionAssert.IsOrdered(result, comparer);
        }