public void FetchQuestions_WhenAttemptRepositoryReturnsQuestionIds_ExcludesAttemptedQuestionIdsFromResult() { // arrange var userId = AutoFixture.Create <string>(); var questionId = AutoFixture.Create <int>(); var attempts = AutoFixture.Build <Attempt>() .With(x => x.QuestionId, questionId) .CreateMany() .ToList(); AutoFixture.Freeze <Mock <IAttemptRepository> >() .Setup(x => x.GetAttempts(It.IsAny <string>())) .Returns(attempts); var previouslyAttemptedQuestions = AutoFixture.Build <QuestionDto>() .With(x => x.QuestionId, questionId) .CreateMany() .ToList(); var notPreviouslyAttemptedQuestions = AutoFixture.CreateMany <QuestionDto>(20).ToList(); previouslyAttemptedQuestions.InsertRange(RandomGenerator.Next(previouslyAttemptedQuestions.Count), notPreviouslyAttemptedQuestions); AutoFixture.Freeze <Mock <IQuestionFilter> >() .Setup(x => x.FilterQuestions(It.IsAny <IEnumerable <QuestionDto> >())) .Returns(previouslyAttemptedQuestions); // act var fetcher = AutoFixture.Create <FilteredLatestQuestionsFetcher>(); var response = fetcher.FetchQuestions(userId); // assert response.Should().BeEquivalentTo(notPreviouslyAttemptedQuestions); }
public void CreateFilter_WhenIRestClientExecuteReturnsResponseWithNullData_ReturnsNull() { // arrange var resourceEnum = AutoFixture.Create <StackExchangeResourceEnum>(); var parameters = AutoFixture.CreateMany <object>().ToList(); var data = AutoFixture.Create <Dictionary <string, object> >(); var client = new Mock <IRestClient>(); var restResponse = AutoFixture.Build <RestResponse <ItemResponseDto <QuestionDto> > >() .Without(x => x.Data) .Create(); client .Setup(x => x.Execute <ItemResponseDto <QuestionDto> >(It.IsAny <IRestRequest>())) .Returns(restResponse); AutoFixture.Freeze <Mock <IRestSharpWrapper> >() .Setup(x => x.CreateRestClient(It.IsAny <string>())) .Returns(client.Object); // act var requestHandler = AutoFixture.Create <StackExchangeRequestHandler>(); var response = requestHandler.Execute <QuestionDto>(resourceEnum, parameters, data); // assert response.Should().BeNull(); }
public void FetchQuestions_Always_CallsIAttemptedQuestionDtoAssemblerAssembleAttemptedQuestionsOnce() { // arrange var userId = AutoFixture.Create <string>(); var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList(); AutoFixture.Freeze <Mock <IStackExchangeClient> >() .Setup(x => x.GetQuestions(It.IsAny <List <int> >())) .Returns(questionDtos); var attemptDtos = AutoFixture.CreateMany <AttemptDto>().ToList(); AutoFixture.Freeze <Mock <IMapper> >() .Setup(x => x.Map <IEnumerable <AttemptDto> >(It.IsAny <IEnumerable <Attempt> >())) .Returns(attemptDtos); var assemblerMock = AutoFixture.Freeze <Mock <IAttemptedQuestionDtoAssembler> >(); // act var fetcher = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>(); fetcher.FetchAttemptedQuestions(userId); // assert assemblerMock.Verify(x => x.AssembleAttemptedQuestions(attemptDtos, questionDtos), Times.Once); }
public void Execute_Always_CallsIRestRequestAddParameterForFilter() { // arrange var resourceEnum = AutoFixture.Create <StackExchangeResourceEnum>(); var parameters = AutoFixture.CreateMany <object>().ToList(); var data = AutoFixture.Create <Dictionary <string, object> >(); var filter = AutoFixture.Create <string>(); AutoFixture.Freeze <Mock <IStackExchangeFilterCreator> >() .Setup(x => x.CreateFilter()) .Returns(filter); var restRequestMock = new Mock <IRestRequest>(); AutoFixture.Freeze <Mock <IRestSharpWrapper> >() .Setup(x => x.CreateRestRequest(It.IsAny <string>())) .Returns(restRequestMock.Object); // act var requestHandler = AutoFixture.Create <StackExchangeRequestHandler>(); requestHandler.Execute <QuestionDto>(resourceEnum, parameters, data); // assert restRequestMock.Verify(x => x.AddParameter("filter", filter), Times.Once); }
public void CreateFilter_WhenIRestClientExecuteReturnsValidResponse_ReturnsResponseData() { // arrange var resourceEnum = AutoFixture.Create <StackExchangeResourceEnum>(); var parameters = AutoFixture.CreateMany <object>().ToList(); var data = AutoFixture.Create <Dictionary <string, object> >(); var client = new Mock <IRestClient>(); var responseData = AutoFixture.Build <ItemResponseDto <QuestionDto> >() .With(x => x.QuotaRemaining, RandomGenerator.Next(1, 100)) .Create(); var restResponse = AutoFixture.Build <RestResponse <ItemResponseDto <QuestionDto> > >() .With(x => x.Data, responseData) .Create(); client .Setup(x => x.Execute <ItemResponseDto <QuestionDto> >(It.IsAny <IRestRequest>())) .Returns(restResponse); AutoFixture.Freeze <Mock <IRestSharpWrapper> >() .Setup(x => x.CreateRestClient(It.IsAny <string>())) .Returns(client.Object); // act var requestHandler = AutoFixture.Create <StackExchangeRequestHandler>(); var response = requestHandler.Execute <QuestionDto>(resourceEnum, parameters, data); // assert response.Should().BeEquivalentTo(responseData.Items); }
public void CalculateScore_GivenIncorrectAnswer_ReturnsRatioOfSameAttemptsDividedByTotalAttemptsOnQuestion() { // arrange var attempt = AutoFixture.Create <Attempt>(); var matchingAttempts = AutoFixture.Build <Attempt>() .With(x => x.AnswerId, attempt.AnswerId) .CreateMany() .ToList(); var nonMatchingAttempts = AutoFixture.CreateMany <Attempt>().ToList(); nonMatchingAttempts.InsertRange(RandomGenerator.Next(nonMatchingAttempts.Count), matchingAttempts); AutoFixture.Freeze <Mock <IAttemptRepository> >() .Setup(x => x.GetAttemptsForQuestion(It.IsAny <int>())) .Returns(nonMatchingAttempts); // act var calculator = AutoFixture.Create <AttemptScoreCalculator>(); var response = calculator.CalculateScore(attempt); // assert var score = (double)(matchingAttempts.Count - 1) / nonMatchingAttempts.Count * 100; response.Should().Be((int)Math.Round(score)); }
public async Task SearchCustomers_When_28_customers_exist_and_pagesize_is_10_and_page_index_is_1_It_should_return_10_customers() { // Arrange await ResetDatabaseAsync(); var numberOfExistingCustomers = 28; var existingCustomers = AutoFixture.CreateMany <Customer>(numberOfExistingCustomers); await AddAsExistingEntitiesAsync(existingCustomers); var query = new SearchCustomersQuery { PageIndex = 1, PageSize = 10 }; // Act var response = await Client.CustomersController().SearchAsync(query); // Assert response.StatusCode.Should().Be(HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync <SearchCustomersQueryResult>(); content.Should().NotBeNull(); content !.Customers.Should().NotBeEmpty(); content.Customers.Should().HaveCount(10); content.PageIndex.Should().Be(query.PageIndex); content.PageSize.Should().Be(query.PageSize); content.TotalItems.Should().Be(numberOfExistingCustomers); content.TotalPages.Should().Be(3); content.HasNextPage.Should().Be(true); content.HasPreviousPage.Should().Be(true); }
public void FetchAnswersForQuestions_Always_SetsAttemptCountEqualToCountFromIAttemptRepositoryGetAttemptsForAnswer() { // arrange var questionId = AutoFixture.Create <int>(); var answerDtos = AutoFixture.CreateMany <AnswerDto>().ToList(); AutoFixture.Freeze <Mock <IStackExchangeClient> >() .Setup(x => x.GetAnswers(It.IsAny <int>())) .Returns(answerDtos); var attempts = AutoFixture.CreateMany <Attempt>().ToList(); AutoFixture.Freeze <Mock <IAttemptRepository> >() .Setup(x => x.GetAttemptsForAnswer(It.IsAny <int>())) .Returns(attempts); // act var controller = AutoFixture.Create <QuestionController>(); controller.FetchAnswersForQuestion(questionId); // assert foreach (var answerDto in answerDtos) { answerDto.AttemptCount.Should().Be(attempts.Count); } }
public void FetchQuestions_Always_CallsIQuestionFilterFilterQuestionsOnce() { // arrange var userId = AutoFixture.Create <string>(); var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList(); AutoFixture.Freeze <Mock <IStackExchangeClient> >() .Setup(x => x.GetLatestQuestions(It.IsAny <int>())) .Returns(questionDtos); var filteredQuestions = AutoFixture.CreateMany <QuestionDto>(20).ToList(); var filterMock = AutoFixture.Freeze <Mock <IQuestionFilter> >(); filterMock .Setup(x => x.FilterQuestions(It.IsAny <IEnumerable <QuestionDto> >())) .Returns(filteredQuestions); // act var fetcher = AutoFixture.Create <FilteredLatestQuestionsFetcher>(); fetcher.FetchQuestions(userId); // assert filterMock.Verify(x => x.FilterQuestions(questionDtos), Times.Once); }
public async void CallTheAccidentStatisticsUrlFor2016() { var accidentStatistics = AutoFixture.CreateMany <AccidentStatistic>(); HttpTest.RespondWithJson(accidentStatistics, 200); await TransportForLondonClient.GetAllAccidentStatistics(2016); HttpTest.ShouldHaveCalled("https://fake-api.tfl.gov.uk/AccidentStats/2016"); }
public async void GetFirstPageAccidentStatistics_WhenThePageIsLessThanOne() { var accidentStatistics = AutoFixture.CreateMany <AccidentStatistic>(); HttpTest.RespondWithJson(accidentStatistics, 200); var actual = await TransportForLondonClient.GetAccidentStatistics(year : 2016, page : -1, pageSize : 5); actual.Should().NotBeNull(); actual.Data.Should().NotBeNull(); actual.Total.Should().Be(3); actual.Page.Should().Be(1); actual.PageSize.Should().Be(3); }
public async void GetFirstPageAccidentStatisticsCorrect() { var accidentStatistics = AutoFixture.CreateMany <AccidentStatistic>(); HttpTest.RespondWithJson(accidentStatistics, 200); var actual = await TransportForLondonClient.GetAccidentStatistics(2016, 1, 5); actual.Should().NotBeNull(); actual.Data.Should().NotBeNull(); actual.Total.Should().Be(3); actual.Page.Should().Be(1); actual.PageSize.Should().Be(3); }
public void WhenFollow() { var subject = Mocker.CreateInstance <FollowInstructions>(); var startingPosition = AutoFixture.Create <Position>(); var instructions = AutoFixture.CreateMany <Instruction>().ToList(); var positionsByIntrustions = instructions.ToDictionary(ins => ins, ins => AutoFixture.Create <Position>()); Mocker.GetMock <IFollowInstruction>() .Setup(fi => fi.Follow(It.IsAny <Position>(), It.Is <Instruction>(ins => instructions.Contains(ins)))) .Returns <Position, Instruction>((pos, ins) => positionsByIntrustions[ins]); var result = subject.Follow(startingPosition, instructions); result.Should().Be(positionsByIntrustions.Last().Value); }
public void FetchLatestQuestions_Always_ReturnsResponseFromIFilteredLatestQuestionsFetcher() { // arrange var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList(); AutoFixture.Freeze <Mock <IFilteredLatestQuestionsFetcher> >() .Setup(x => x.FetchQuestions(It.IsAny <string>())) .Returns(questionDtos); // act var controller = AutoFixture.Create <QuestionController>(); var response = controller.FetchLatestQuestions(); // assert response.Should().BeEquivalentTo(questionDtos); }
public void WhenGet() { AutoFixture.Register(() => Guid.NewGuid().ToString().Substring(0, 1)); var subject = Mocker.CreateInstance <GetInstructions>(); var instructions = AutoFixture.CreateMany <Instruction>(); var instructionsDictionary = instructions.ToDictionary(ins => $"{ins.Direction}{ins.Distance}", ins => ins); var input = string.Join(", ", instructionsDictionary.Keys); Mocker.GetMock <IGetInstruction>() .Setup(gi => gi.Get(It.IsAny <string>())) .Returns <string>(s => instructionsDictionary[s]); var result = subject.Get(input); result.Should().Equal(instructionsDictionary.Values); }
public void FetchPreviousQuestions_Always_ReturnsResponseFromIPreviouslyAttemptedQuestionFetcher() { // arrange var attemptQuestionDtos = AutoFixture.CreateMany <AttemptedQuestionDto>().ToList(); AutoFixture.Freeze <Mock <IPreviouslyAttemptedQuestionFetcher> >() .Setup(x => x.FetchAttemptedQuestions(It.IsAny <string>())) .Returns(attemptQuestionDtos); // act var controller = AutoFixture.Create <AttemptController>(); var response = controller.FetchPreviousQuestions(); // assert response.Should().BeEquivalentTo(attemptQuestionDtos); }
public void AssembleAttemptedQuestions_WhenQuestionFilterReturnsNull_ReturnsEmptyList() { // arrange var attemptDtos = AutoFixture.CreateMany <AttemptDto>().ToList(); var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList(); AutoFixture.Freeze <Mock <IQuestionFilter> >() .Setup(x => x.GetQuestionDtoById(It.IsAny <IEnumerable <QuestionDto> >(), It.IsAny <int>())) .Returns((QuestionDto)null); // act var assembler = AutoFixture.Create <AttemptedQuestionDtoAssembler>(); var response = assembler.AssembleAttemptedQuestions(attemptDtos, questionDtos); // assert response.Should().BeEmpty(); }
public void Execute_Always_CallsIRestSharpWrapperCreateRestClientOnce() { // arrange var resourceEnum = AutoFixture.Create <StackExchangeResourceEnum>(); var parameters = AutoFixture.CreateMany <object>().ToList(); var data = AutoFixture.Create <Dictionary <string, object> >(); var restSharpWrapperMock = AutoFixture.Freeze <Mock <IRestSharpWrapper> >(); // act var requestHandler = AutoFixture.Create <StackExchangeRequestHandler>(); requestHandler.Execute <QuestionDto>(resourceEnum, parameters, data); // assert restSharpWrapperMock.Verify(x => x.CreateRestClient("https://api.stackexchange.com/2.2"), Times.Once); }
public void Execute_Always_CallsIStackExchangeFilterCreator() { // arrange var resourceEnum = AutoFixture.Create <StackExchangeResourceEnum>(); var parameters = AutoFixture.CreateMany <object>().ToList(); var data = AutoFixture.Create <Dictionary <string, object> >(); var filterCreatorMock = AutoFixture.Freeze <Mock <IStackExchangeFilterCreator> >(); // act var requestHandler = AutoFixture.Create <StackExchangeRequestHandler>(); requestHandler.Execute <QuestionDto>(resourceEnum, parameters, data); // assert filterCreatorMock.Verify(x => x.CreateFilter(), Times.Once); }
public void FetchAnswersForQuestion_Always_ReturnsResponseFromIStackExchangeClient() { // arrange var questionId = AutoFixture.Create <int>(); var answerDtos = AutoFixture.CreateMany <AnswerDto>().ToList(); AutoFixture.Freeze <Mock <IStackExchangeClient> >() .Setup(x => x.GetAnswers(It.IsAny <int>())) .Returns(answerDtos); // act var controller = AutoFixture.Create <QuestionController>(); var response = controller.FetchAnswersForQuestion(questionId); // assert response.Should().BeEquivalentTo(answerDtos); }
public void FetchQuestions_Always_ReturnsResponseFromIAttemptedQuestionDtoAssemblerOrderedByAttemptDate() { // arrange var userId = AutoFixture.Create <string>(); var attemptedQuestionDtos = AutoFixture.CreateMany <AttemptedQuestionDto>().ToList(); AutoFixture.Freeze <Mock <IAttemptedQuestionDtoAssembler> >() .Setup(x => x.AssembleAttemptedQuestions(It.IsAny <IEnumerable <AttemptDto> >(), It.IsAny <IEnumerable <QuestionDto> >())) .Returns(attemptedQuestionDtos); // act var fetcher = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>(); var response = fetcher.FetchAttemptedQuestions(userId); // assert response.Should().BeInAscendingOrder(attemptedQuestion => attemptedQuestion.AttemptDto.AttemptDate); }
private IEnumerable <RdbNet> CreateTestData(int length) { var data = new List <RdbNet>(); while (length > 0) { var current = new RdbNet { name = AutoFixture.Create <string>(), pins = AutoFixture.CreateMany <Pin>(), prop = AutoFixture.Create <Prop>() }; data.Add(current); length--; } return(data); }
public void FetchQuestions_WhenQuestionDtosReturnedIsGreaterThan20_ReturnsOnly20() { // arrange var userId = AutoFixture.Create <string>(); var filteredQuestions = AutoFixture.CreateMany <QuestionDto>(RandomGenerator.Next(20, 100)).ToList(); AutoFixture.Freeze <Mock <IQuestionFilter> >() .Setup(x => x.FilterQuestions(It.IsAny <IEnumerable <QuestionDto> >())) .Returns(filteredQuestions); // act var fetcher = AutoFixture.Create <FilteredLatestQuestionsFetcher>(); var response = fetcher.FetchQuestions(userId); // assert response.Should().HaveCount(20); }
public void AssembleAttemptedQuestions_Always_CallsIQuestionFilterGetQuestionDtoByIdOncePerAttempt() { // arrange var attemptDtos = AutoFixture.CreateMany <AttemptDto>().ToList(); var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList(); var filterMock = AutoFixture.Freeze <Mock <IQuestionFilter> >(); // act var assembler = AutoFixture.Create <AttemptedQuestionDtoAssembler>(); assembler.AssembleAttemptedQuestions(attemptDtos, questionDtos); // assert foreach (var attempt in attemptDtos) { filterMock.Verify(x => x.GetQuestionDtoById(questionDtos, attempt.QuestionId), Times.Once); } }
public void GetQuestionDtoById_Always_ReturnsQuestionsWhereAnswerCountGreaterThan1AndAcceptedAnswerIdHasValue() { // arrange var questionId = AutoFixture.Create <int>(); var matchingQuestionDtos = AutoFixture.Build <QuestionDto>() .With(x => x.QuestionId, questionId) .Create(); var nonMatchingQuestionDtos = AutoFixture.CreateMany <QuestionDto>() .ToList(); nonMatchingQuestionDtos.Insert(RandomGenerator.Next(nonMatchingQuestionDtos.Count), matchingQuestionDtos); // act var filter = AutoFixture.Create <QuestionFilter>(); var response = filter.GetQuestionDtoById(nonMatchingQuestionDtos, questionId); // assert response.Should().Be(matchingQuestionDtos); }
public void FetchQuestions_Always_CallsIMapperMapAttemptDtoOnce() { // arrange var userId = AutoFixture.Create <string>(); var attempts = AutoFixture.CreateMany <Attempt>().ToList(); AutoFixture.Freeze <Mock <IAttemptRepository> >() .Setup(x => x.GetAttempts(It.IsAny <string>())) .Returns(attempts); var mapperMock = AutoFixture.Freeze <Mock <IMapper> >(); // act var fetcher = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>(); fetcher.FetchAttemptedQuestions(userId); // assert mapperMock.Verify(x => x.Map <IEnumerable <AttemptDto> >(attempts), Times.Once); }
public void GetAttemptsForQuestion_Always_CallsIMapperMapAttemptDtoOnce() { // arrange var questionId = AutoFixture.Create <int>(); var attempts = AutoFixture.CreateMany <Attempt>().ToList(); AutoFixture.Freeze <Mock <IAttemptRepository> >() .Setup(x => x.GetAttemptsForQuestion(It.IsAny <int>())) .Returns(attempts); var mapperMock = AutoFixture.Freeze <Mock <IMapper> >(); // act var controller = AutoFixture.Create <QuestionController>(); controller.GetAttemptsForQuestion(questionId); // assert mapperMock.Verify(x => x.Map <IEnumerable <AttemptDto> >(attempts), Times.Once); }
public void AssembleAttemptedQuestions_WhenMapperAndQuestionFilterReturnNotNull_ReturnsAttemptedQuestionDtos() { // arrange var attemptDtos = AutoFixture.CreateMany <AttemptDto>().ToList(); var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList(); var questionDto = AutoFixture.Create <QuestionDto>(); AutoFixture.Freeze <Mock <IQuestionFilter> >() .Setup(x => x.GetQuestionDtoById(It.IsAny <IEnumerable <QuestionDto> >(), It.IsAny <int>())) .Returns(questionDto); // act var assembler = AutoFixture.Create <AttemptedQuestionDtoAssembler>(); var response = assembler.AssembleAttemptedQuestions(attemptDtos, questionDtos); // assert foreach (var attemptDto in attemptDtos) { response.Should().Contain(x => x.AttemptDto == attemptDto && x.QuestionDto == questionDto); } }
public void FetchQuestions_Always_CallsIStackExchangeClientGetQuestionsOnce() { // arrange var userId = AutoFixture.Create <string>(); var attempts = AutoFixture.CreateMany <Attempt>().ToList(); AutoFixture.Freeze <Mock <IAttemptRepository> >() .Setup(x => x.GetAttempts(It.IsAny <string>())) .Returns(attempts); var attemptIds = attempts.Select(attempt => attempt.QuestionId).ToList(); var clientMock = AutoFixture.Freeze <Mock <IStackExchangeClient> >(); // act var fetcher = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>(); fetcher.FetchAttemptedQuestions(userId); // assert clientMock.Verify(x => x.GetQuestions(attemptIds), Times.Once); }
public void FetchQuestions_Always_CallsIAttemptRepositoryGetAttemptsOnce() { // arrange var userId = AutoFixture.Create <string>(); var repositoryMock = AutoFixture.Freeze <Mock <IAttemptRepository> >(); var filteredQuestions = AutoFixture.CreateMany <QuestionDto>(20).ToList(); AutoFixture.Freeze <Mock <IQuestionFilter> >() .Setup(x => x.FilterQuestions(It.IsAny <IEnumerable <QuestionDto> >())) .Returns(filteredQuestions); // act var fetcher = AutoFixture.Create <FilteredLatestQuestionsFetcher>(); fetcher.FetchQuestions(userId); // assert repositoryMock.Verify(x => x.GetAttempts(userId), Times.Once); }