Beispiel #1
0
        public async Task When2UsersVotesForSameArticleTheAverageShoultBeCorrect()
        {
            var list     = new List <Vote>();
            var mockRepo = new Mock <IRepository <Vote> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Vote>())).Callback((Vote vote) => list.Add(vote));

            var service = new VotesService(mockRepo.Object);

            await service.VoteAsync(2, "Gosho", 5);

            await service.VoteAsync(2, "Tosho", 1);

            await service.VoteAsync(2, "Gosho", 2);

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

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