예제 #1
0
        public async Task GivenRandomNumbers_Create_ReturnsMatchWithContestants()
        {
            // Arrange
            firstRandomNumber  = 5;
            secondRandomNumber = 3;
            _stubContestantRepository.Setup(x => x.Get(WarId, firstRandomNumber)).Returns(Task.FromResult(_contestant1));
            _stubContestantRepository.Setup(x => x.Get(WarId, secondRandomNumber)).Returns(Task.FromResult(_contestant2));

            // Act
            var result = await _factory.Create(WarId, _userId);

            // Assert
            VerifyResult(result);
        }
예제 #2
0
        public async Task GivenMultipleCandidates_EachOneIsReturnedInStatisticallyEvenNumber()
        {
            // Arrange
            const int         warId                = 987;
            const int         contestantCount      = 10;
            const int         iterations           = 100000;
            const float       lowerBoundPercentage = 0.9f;
            const float       upperBoundPercentage = 1.1f;
            List <Contestant> contestants          = CreateContestantsCollection(contestantCount);

            SetupContestantRepository(warId, contestants);
            var factory = new RandomMatchStrategy(_stubMatchRepository.Object, _stubContestantRepository.Object);
            var matches = new List <MatchWithContestants>();

            // Act
            for (var i = 0; i < iterations; i++)
            {
                var match = await factory.Create(warId, _userId);

                matches.Add(match);
            }

            // Assert
            var groupedByContestant1 = matches.GroupBy(x => x.Contestant1.Id);

            foreach (var g in groupedByContestant1)
            {
                const float expectedCount = (iterations / contestantCount);
                var         actualCount   = (float)g.Count();
                actualCount.Should().BeInRange(expectedCount * lowerBoundPercentage, expectedCount * upperBoundPercentage);
                float expectedRivalCounts  = actualCount / (contestantCount - 1);
                var   groupedByContestant2 = g.GroupBy(x => x.Contestant2.Id);
                groupedByContestant2.Select(x => (float)x.Count()).Should().OnlyContain(x => x >= expectedRivalCounts * lowerBoundPercentage && x <= expectedRivalCounts * upperBoundPercentage);
            }
        }