Exemplo n.º 1
0
        public async Task Then_The_Api_Is_Called_With_The_Request_And_The_Cohort_Is_Returned(
            GetCohortQuery query,
            GetCohortResponse apiResponse,
            [Frozen] Mock <ICommitmentsV2ApiClient <CommitmentsV2ApiConfiguration> > apiClient,
            GetCohortQueryHandler handler
            )
        {
            apiClient.Setup(x => x.Get <GetCohortResponse>(It.Is <GetCohortRequest>(x => x.CohortId == query.CohortId))).ReturnsAsync(apiResponse);

            var actual = await handler.Handle(query, CancellationToken.None);

            actual.Should().BeEquivalentTo((GetCohortResult)apiResponse);
        }
        public async Task Then_Gets_Cohort_From_CommitmentsV2_Api(
            GetCohortQuery query,
            GetCohortResponse apiResponse,
            [Frozen] Mock <ICommitmentsV2ApiClient <CommitmentsV2ApiConfiguration> > mockApiClient,
            GetCohortQueryHandler handler)
        {
            mockApiClient
            .Setup(client => client.Get <GetCohortResponse>(It.Is <GetCohortRequest>(request => request.CohortId == query.CohortId)))
            .ReturnsAsync(apiResponse);

            var result = await handler.Handle(query, CancellationToken.None);

            result.Cohort.Should().BeEquivalentTo(apiResponse);
        }
        public void ThenWillThrowExceptionIfValidationFails(
            GetCohortQuery query,
            [Frozen] Mock <ICommitmentService> mockCommitmentService,
            [Frozen] Mock <IValidator <GetCohortQuery> > mockValidator,
            GetCohortQueryHandler handler)
        {
            //Arrange
            mockValidator.Setup(v => v.ValidateAsync(It.IsAny <GetCohortQuery>())).ReturnsAsync(new ValidationResult
            {
                ValidationDictionary = new Dictionary <string, string> {
                    { "Error", "Test Error" }
                }
            });

            //Act + Assert
            Assert.ThrowsAsync <ValidationException>(() => handler.Handle(query, CancellationToken.None));
        }
        public void ThenWillThrowExceptionIfOneOccurs(
            GetCohortQuery query,
            Exception exception,
            [Frozen] Mock <ICommitmentService> mockCommitmentService,
            [Frozen] Mock <IValidator <GetCohortQuery> > mockValidator,
            GetCohortQueryHandler handler)
        {
            //Arrange
            mockValidator.Setup(v => v.ValidateAsync(It.IsAny <GetCohortQuery>()))
            .ReturnsAsync(new ValidationResult {
                ValidationDictionary = new Dictionary <string, string>()
            });

            mockCommitmentService.Setup(s => s.GetCohort(It.IsAny <long>()))
            .ThrowsAsync(exception);

            //Act
            var actualException = Assert.ThrowsAsync <Exception>(() => handler.Handle(query, CancellationToken.None));

            //Assert
            Assert.AreEqual(exception, actualException);
        }
        public async Task ThenWillUseCorrectCohortId(
            Cohort expectedCohort,
            [Frozen] GetCohortQuery query,
            [Frozen] Mock <ICommitmentService> mockCommitmentService,
            [Frozen] Mock <IValidator <GetCohortQuery> > mockValidator,
            GetCohortQueryHandler handler)
        {
            //Arrange
            mockValidator.Setup(v => v.ValidateAsync(It.IsAny <GetCohortQuery>()))
            .ReturnsAsync(new ValidationResult {
                ValidationDictionary = new Dictionary <string, string>()
            });

            mockCommitmentService.Setup(s => s.GetCohort(It.IsAny <long>()))
            .ReturnsAsync(expectedCohort);

            //Act
            await handler.Handle(query, CancellationToken.None);

            //Assert
            mockCommitmentService.Verify(s => s.GetCohort(query.CohortId), Times.Once);
        }
        public async Task ThenWillGetCohortFromService(
            GetCohortQuery query,
            Cohort expectedCohort,
            [Frozen] Mock <ICommitmentService> mockCommitmentService,
            [Frozen] Mock <IValidator <GetCohortQuery> > mockValidator,
            GetCohortQueryHandler handler)
        {
            //Arrange
            mockValidator.Setup(v => v.ValidateAsync(It.IsAny <GetCohortQuery>()))
            .ReturnsAsync(new ValidationResult {
                ValidationDictionary = new Dictionary <string, string>()
            });

            mockCommitmentService.Setup(s => s.GetCohort(It.IsAny <long>()))
            .ReturnsAsync(expectedCohort);

            //Act
            var result = await handler.Handle(query, CancellationToken.None);

            //Assert
            Assert.AreEqual(expectedCohort, result.Cohort);
        }