コード例 #1
0
        public async Task When2UsersRateForTheSameQuestionTheAverageVoteShouldBeCorrect()
        {
            var list     = new List <Rating>();
            var mockRepo = new Mock <IRepository <Rating> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Rating>())).Callback(
                (Rating rating) => list.Add(rating));
            var service = new RatingsService(mockRepo.Object);

            await service.SetRatingAsync(2, "Niki", 5);

            await service.SetRatingAsync(2, "Pesho", 1);

            await service.SetRatingAsync(2, "Niki", 2);

            mockRepo.Verify(x => x.AddAsync(It.IsAny <Rating>()), Times.Exactly(2));

            Assert.Equal(2, list.Count);
            Assert.Equal(1.5, service.GetAverageRatings(2));
        }