public async Task TwoUpVotesShouldCountOnce() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfRepository <Vote>(new ApplicationDbContext(options.Options)); var service = new VotesService(repository); await service.VoteAsync(1, "1", true); await service.VoteAsync(1, "1", true); await service.VoteAsync(1, "1", true); await service.VoteAsync(1, "2", true); await service.VoteAsync(1, "2", true); await service.VoteAsync(1, "2", true); var votes = service.GetVotes(1); Assert.Equal(2, votes); }
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); }
public async Task SetVoteShouldApplyCorrectValues() { 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", 4); var vote = await repository.All().FirstAsync(x => x.CoachId == "coachId"); Assert.Equal("coachId", vote.CoachId); Assert.Equal("clientId", vote.ClientId); Assert.Equal(4, vote.Value); }
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); }
public async Task WhenUserVoteOnceTheVoteIsAdded() { var list = new List <Vote>(); var mockRepository = new Mock <IDeletableEntityRepository <Vote> >(); mockRepository .Setup(x => x.All()) .Returns(list.AsQueryable()); mockRepository .Setup(x => x.AddAsync(It.IsAny <Vote>())) .Callback((Vote vote) => list.Add(vote)); var service = new VotesService(mockRepository.Object); await service.CreateAsync(new CreateVoteDTO { ForUserId = "a", VoteValue = true, VoterId = "b", }); Assert.Single(list.Where(x => !x.IsDeleted && x.ForUserId == "a")); }
public async Task TwoDownVotesShouldCountOnce() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MemoryDb"); var repository = new EfRepository <Vote>(new ApplicationDbContext(options.Options)); var service = new VotesService(repository); // voting 100 times with first User for (int i = 0; i < 100; i++) { await service.VoteAsync(1, "1", false); // down vote } // voting 100 times with second User for (int i = 0; i < 100; i++) { await service.VoteAsync(1, "2", false); // down vote } var votesCount = service.GetVotes(1); Assert.Equal(-2, votesCount); }
public ResultsController(VotesService votesService) { _votesService = votesService; }
public VotesServiceTests() { this.picturesList = new List <Picture>(); this.picturesRepo = new Mock <IDeletableEntityRepository <Picture> >(); this.picturesRepo.Setup(x => x.All()).Returns(this.picturesList.AsQueryable()); this.picturesRepo.Setup(x => x.AddAsync(It.IsAny <Picture>())).Callback((Picture picture) => this.picturesList.Add(picture)); this.picturesService = new PicturesService(this.picturesRepo.Object); this.attachmentsList = new List <Attachment>(); this.attachmentsRepo = new Mock <IDeletableEntityRepository <Attachment> >(); this.attachmentsRepo.Setup(x => x.All()).Returns(this.attachmentsList.AsQueryable()); this.attachmentsRepo.Setup(x => x.AddAsync(It.IsAny <Attachment>())).Callback((Attachment att) => this.attachmentsList.Add(att)); this.issueAttachmentsList = new List <IssueAttachment>(); this.issueAttachmentsRepo = new Mock <IRepository <IssueAttachment> >(); this.issueAttachmentsRepo.Setup(x => x.All()).Returns(this.issueAttachmentsList.AsQueryable()); this.issueAttachmentsRepo.Setup(x => x.AddAsync(It.IsAny <IssueAttachment>())).Callback((IssueAttachment issueAtt) => this.issueAttachmentsList.Add(issueAtt)); this.issuesList = new List <Issue>(); this.issuesRepo = new Mock <IDeletableEntityRepository <Issue> >(); this.issuesRepo.Setup(x => x.All()).Returns(this.issuesList.AsQueryable()); this.issuesRepo.Setup(x => x.AddAsync(It.IsAny <Issue>())).Callback((Issue issue) => this.issuesList.Add(issue)); this.issueTagsList = new List <IssueTag>(); this.issueTagsRepo = new Mock <IRepository <IssueTag> >(); this.issueTagsRepo.Setup(x => x.All()).Returns(this.issueTagsList.AsQueryable()); this.issueTagsRepo.Setup(x => x.AddAsync(It.IsAny <IssueTag>())).Callback((IssueTag issueTag) => this.issueTagsList.Add(issueTag)); this.tagsList = new List <Tag>(); this.tagsRepo = new Mock <IRepository <Tag> >(); this.tagsRepo.Setup(x => x.All()).Returns(this.tagsList.AsQueryable()); this.tagsRepo.Setup(x => x.AddAsync(It.IsAny <Tag>())).Callback((Tag tag) => this.tagsList.Add(tag)); this.tagsService = new TagsService(this.tagsRepo.Object); this.issueTagsService = new IssueTagsService(this.issueTagsRepo.Object, new StringOperationsServices(), this.tagsService); this.citizensList = new List <Citizen>(); this.citizensRepo = new Mock <IDeletableEntityRepository <Citizen> >(); this.citizensRepo.Setup(x => x.All()).Returns(this.citizensList.AsQueryable()); this.citizensRepo.Setup(x => x.AddAsync(It.IsAny <Citizen>())).Callback((Citizen citizen) => this.citizensList.Add(citizen)); this.citizensService = new CitizensService(this.citizensRepo.Object); this.addressList = new List <Address>(); this.addressesRepo = new Mock <IDeletableEntityRepository <Address> >(); this.addressesRepo.Setup(x => x.All()).Returns(this.addressList.AsQueryable()); this.addressesRepo.Setup(x => x.AddAsync(It.IsAny <Address>())).Callback((Address address) => this.addressList.Add(address)); this.addressesService = new AddressesService(this.addressesRepo.Object, this.citiesService); this.citiesList = new List <City>(); this.citiesRepo = new Mock <IRepository <City> >(); this.citiesRepo.Setup(x => x.All()).Returns(this.citiesList.AsQueryable()); this.citiesRepo.Setup(x => x.AddAsync(It.IsAny <City>())).Callback((City city) => this.citiesList.Add(city)); this.citiesService = new CitiesService(this.citiesRepo.Object); this.votesList = new List <Vote>(); this.votesRepo = new Mock <IRepository <Vote> >(); this.votesRepo.Setup(x => x.All()).Returns(this.votesList.AsQueryable()); this.votesRepo.Setup(x => x.AllAsNoTracking()).Returns(this.votesList.AsQueryable()); this.votesRepo.Setup(x => x.Delete(It.IsAny <Vote>())).Callback((Vote vote) => this.votesList.Remove(vote)); this.votesRepo.Setup(x => x.AddAsync(It.IsAny <Vote>())).Callback((Vote vote) => this.votesList.Add(vote)); this.issuesService = new IssuesService( this.issuesRepo.Object, this.issueAttachmentsRepo.Object, this.attachmentsRepo.Object, this.citizensRepo.Object, this.citizensService, this.picturesService, this.addressesService, this.issueTagsService); this.votesService = new VotesService(this.votesRepo.Object, this.citizensService, this.issuesService); }
public VotesController(VotesService votesService, CandidateService candidateService) { _votesService = votesService; _candidateService = candidateService; }