예제 #1
0
        public async Task When2UsersVoteForTheSameRecipeTheAverageVoteShouldBeCorrect()
        {
            var votesList = new List <Vote>();

            var mockRepo = new Mock <IRepository <Vote> >();

            mockRepo.Setup(method => method.All())
            .Returns(votesList.AsQueryable());

            mockRepo.Setup(m => m.AddAsync(It.IsAny <Vote>()))
            .Callback((Vote vote) => votesList.Add(vote));

            var service = new VotesService(mockRepo.Object);

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

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

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

            // mockRepo.Verify(m => m.All(), Times.Exactly(3));
            // Можем да проверим колко пъти е извикан даден метод. В случая, дали метода All() е извикан точно три пъти.

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

            Assert.Equal(2, votesList.Count());
            Assert.Equal(1.5, service.GetAverageVotes(2));
        }
        public async Task When2UsersVoteForTheSameRecipeTheAverageVoteShouldBeCorrect()
        {
            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.SetVoteAsync(2, "Niki", 5);

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

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

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

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

            // TODO: Only 1 vote;

            // TODO: if vote is 5
        }
        public async Task WhenUserVote2TimesOnly1VoteShouldBeCounted()
        {
            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.SetVoteAsync(1, "1", 1);

            await service.SetVoteAsync(1, "1", 5);

            await service.SetVoteAsync(1, "1", 5);

            await service.SetVoteAsync(1, "1", 5);

            await service.SetVoteAsync(1, "1", 5);

            Assert.Equal(1, list.Count);
            Assert.Equal(5, list.First().Value);

            // TODO: Only 1 vote;

            // TODO: if vote is 5
        }
예제 #4
0
        public async Task WhenTwoUsersVoteForTheSameRecipeTheAverageVoteShouldBeCorrect()
        {
            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.SetVoteAsync(1, "1", 4);

            await service.SetVoteAsync(1, "2", 5);

            // Check if the method AddAsync is called exactly 2 times
            mockRepo.Verify(x => x.AddAsync(It.IsAny <Vote>()), Times.Exactly(2));

            Assert.Equal(4.5, service.GetAverageVotes(1));
        }
예제 #5
0
        public async Task SetVoteShouldChangeVoteValueCorrectly()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repository       = new EfRepository <Vote>(dbContext);
            var clientRepository = new EfDeletableEntityRepository <Client>(dbContext);

            var client = new Client
            {
                Id             = "clientId",
                Name           = "random",
                Phone          = "088",
                PositionPlayed = PositionName.Center,
                User           = new ApplicationUser {
                    Id = "clientuserId", Email = "*****@*****.**"
                },
                UserId = "clientuserId",
            };

            await dbContext.Clients.AddAsync(client);

            await dbContext.SaveChangesAsync();

            var workoutslistRepository = new EfDeletableEntityRepository <WorkoutsList>(dbContext);

            var clientsService = new ClientsService(clientRepository, workoutslistRepository);
            var service        = new VotesService(repository, clientsService);

            await service.SetVoteAsync("coachId", "clientuserId", 2);

            await service.SetVoteAsync("coachId", "clientuserId", 5);

            var count = repository.All();
            var vote  = await repository.All().FirstAsync(x => x.CoachId == "coachId");

            Assert.Equal(1, await count.CountAsync());
            Assert.Equal(5, vote.Value);
        }
예제 #6
0
        public async Task WhenUserVotesMoreThanOneTimeForOneRecipeOnlyTheLastShouldBeCounted()
        {
            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.SetVoteAsync(1, "1", 1);

            await service.SetVoteAsync(1, "1", 5);

            await service.SetVoteAsync(1, "1", 4);

            await service.SetVoteAsync(1, "1", 3);

            await service.SetVoteAsync(1, "1", 2);

            Assert.Equal(1, list.Count());
            Assert.Equal(2, list.First().Value);
        }
예제 #7
0
        public async Task WhenUserVotesTwoTimesOnlyOneVoteShouldBeCounted()
        {
            var list = new List <Vote>();

            this.votesRepository
            .Setup(x => x.All())
            .Returns(list.AsQueryable());

            var service = new VotesService(this.votesRepository.Object);

            list.Add(new Vote {
                MealId = 1, UserId = "userId", Value = 5
            });

            await service.SetVoteAsync(1, "userId", 1);

            await service.SetVoteAsync(1, "userId", 5);

            var result = list.FirstOrDefault(x => x.MealId == 1);

            Assert.Single(list);
            Assert.Equal(5, result.Value);
        }
예제 #8
0
        public async Task WhenUserVotes2TimesOnly1VoteShouldBeCounted()
        {
            var votesList = new List <Vote>();
            // Това все едно в момента ни е таблицата Votes от базата данни.

            var mockRepo = new Mock <IRepository <Vote> >();

            mockRepo.Setup(method => method.All())
            .Returns(votesList.AsQueryable());
            // Когато някой ти извика медода .All(), ти му върни votesList.AsQueryable().

            mockRepo.Setup(m => m.AddAsync(It.IsAny <Vote>()))
            .Callback((Vote vote) => votesList.Add(vote));
            // Когато имаме параметри на метода, в случая AddAsync иска да му се подаде Vote, със It.IsAny му казваме, ползвай това в метода, независимо как е подадено.

            // var repo = new FakeVotesRepository();
            // var service = new VotesService(repo);

            var service = new VotesService(mockRepo.Object);

            await service.SetVoteAsync(1, "1", 1);

            await service.SetVoteAsync(1, "1", 5);

            await service.SetVoteAsync(1, "1", 5);

            await service.SetVoteAsync(1, "1", 5);

            await service.SetVoteAsync(1, "1", 5);

            // Assert.Equal(1, repo.All().Count());
            // Assert.Equal(5, repo.All().First().Value);

            Assert.Equal(1, votesList.Count());
            Assert.Equal(5, votesList.First().Value);
        }
예제 #9
0
        public async Task TestCreateNewVote()
        {
            ApplicationDbContext db = GetDb();

            var voteRepository = new EfRepository <Vote>(db);

            var service = new VotesService(voteRepository);

            await service.SetVoteAsync(1, "userId", 5);

            var result = db.Votes.FirstOrDefault(x => x.MealId == 1 && x.UserId == "userId");

            Assert.Single(db.Votes.Where(x => x.MealId == 1));
            Assert.Single(db.Votes.Where(x => x.UserId == "userId"));
            Assert.Equal(5, result.Value);
        }