コード例 #1
0
        public void GetMatchingTestScopedInteraction_WithOneMatchingTestScopedInteraction_DoesNotThrowAPactFailureException()
        {
            var expectedInteraction = new ProviderServiceInteraction
            {
                Description = "My description",
                Request     = new ProviderServiceRequest
                {
                    Method = HttpVerb.Head,
                    Path   = "/tester"
                },
                Response = new ProviderServiceResponse
                {
                    Status = (int)HttpStatusCode.NoContent
                }
            };

            var repo = GetSubject();

            _mockComparer
            .Compare(expectedInteraction.Request, expectedInteraction.Request)
            .Returns(new ComparisonResult());

            repo.AddInteraction(expectedInteraction);

            var interaction = repo.GetMatchingTestScopedInteraction(expectedInteraction.Request);

            Assert.Equal(expectedInteraction, interaction);
        }
コード例 #2
0
        public ProviderServiceInteraction GetMatchingTestScopedInteraction(ProviderServiceRequest request)
        {
            if (TestScopedInteractions == null || !TestScopedInteractions.Any())
            {
                throw new PactFailureException(String.Format("No interaction found for {0} {1}.", request.Method.ToString().ToUpperInvariant(), request.Path));
            }

            var matchingInteractions = new List <ProviderServiceInteraction>();

            foreach (var testScopedInteraction in TestScopedInteractions)
            {
                var requestComparisonResult = _requestComparer.Compare(testScopedInteraction.Request, request);
                if (requestComparisonResult != null && !requestComparisonResult.HasFailure)
                {
                    matchingInteractions.Add(testScopedInteraction);
                }
            }

            if (matchingInteractions == null || !matchingInteractions.Any())
            {
                throw new PactFailureException(String.Format("No interaction found for {0} {1}.", request.Method.ToString().ToUpperInvariant(), request.Path));
            }

            if (matchingInteractions.Count() > 1)
            {
                throw new PactFailureException(String.Format("More than one interaction found for {0} {1}.", request.Method.ToString().ToUpperInvariant(), request.Path));
            }

            return(matchingInteractions.Single());
        }
コード例 #3
0
        private Response HandlePactRequest(NancyContext context)
        {
            var actualRequest = _requestMapper.Convert(context.Request);

            var matchingInteraction = context.GetMatchingInteraction(actualRequest.Method, actualRequest.Path);

            matchingInteraction.IncrementUsage();

            _requestComparer.Compare(matchingInteraction.Request, actualRequest);

            return(_responseMapper.Convert(matchingInteraction.Response));
        }
コード例 #4
0
        public void Verify()
        {
            _requestComparer.Compare(Expected, Actual);

            if (Match)
            {
                _reporter.DidNotReceive().ReportError(Arg.Any <string>(), Arg.Any <object>(), Arg.Any <object>());
            }
            else
            {
                _reporter.Received(1).ReportError(Arg.Any <string>(), Arg.Any <object>(), Arg.Any <object>());
            }
        }
コード例 #5
0
ファイル: RequestTestCase.cs プロジェクト: ymartin/pact-net
        public void Verify()
        {
            var result = _requestComparer.Compare(Expected, Actual);

            if (Match)
            {
                Assert.False(result.HasFailure, "There should not be any errors");
            }
            else
            {
                Assert.Equal(1, result.Failures.Count());
            }
        }
コード例 #6
0
        private Response HandleGetInteractionsVerificationRequest(NancyContext context)
        {
            //Check all registered interactions have been used once and only once
            var registeredInteractions = context.GetMockInteractions().ToList();

            if (registeredInteractions.Any())
            {
                foreach (var registeredInteraction in registeredInteractions)
                {
                    var interactionUsages = _mockProviderRepository.HandledRequests.Where(x => x.MatchedInteraction == registeredInteraction).ToList();

                    if (interactionUsages == null || !interactionUsages.Any())
                    {
                        _reporter.ReportError(String.Format("Registered mock interaction with description '{0}' and provider state '{1}', was not used by the test.", registeredInteraction.Description, registeredInteraction.ProviderState));
                    }
                    else if (interactionUsages.Count() > 1)
                    {
                        _reporter.ReportError(String.Format("Registered mock interaction with description '{0}' and provider state '{1}', was used {2} time/s by the test.", registeredInteraction.Description, registeredInteraction.ProviderState, interactionUsages.Count()));
                    }
                }
            }
            else
            {
                if (_mockProviderRepository.HandledRequests != null && _mockProviderRepository.HandledRequests.Any())
                {
                    _reporter.ReportError("No mock interactions were registered, however the mock provider service was called.");
                }
            }

            //Check all handled requests actually match the registered interaction
            if (_mockProviderRepository.HandledRequests != null &&
                _mockProviderRepository.HandledRequests.Any())
            {
                foreach (var handledRequest in _mockProviderRepository.HandledRequests)
                {
                    _requestComparer.Compare(handledRequest.MatchedInteraction.Request, handledRequest.ActualRequest);
                }
            }

            try
            {
                _reporter.ThrowIfAnyErrors();
            }
            catch (Exception ex)
            {
                _reporter.ClearErrors();
                return(GenerateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }

            return(GenerateResponse(HttpStatusCode.OK, "Successfully verified mock provider interactions."));
        }
コード例 #7
0
        public ProviderServiceInteraction GetMatchingTestScopedInteraction(ProviderServiceRequest request)
        {
            if (TestScopedInteractions == null || !TestScopedInteractions.Any())
            {
                throw new PactFailureException("No mock interactions have been registered");
            }

            var matchingInteractions = new List <ProviderServiceInteraction>();

            foreach (var testScopedInteraction in TestScopedInteractions)
            {
                _requestComparer.Compare(testScopedInteraction.Request, request);

                try
                {
                    _reporter.ThrowIfAnyErrors();
                }
                catch (Exception)
                {
                    _reporter.ClearErrors();
                    continue;
                }

                matchingInteractions.Add(testScopedInteraction);
            }

            if (matchingInteractions == null || !matchingInteractions.Any())
            {
                throw new PactFailureException("No matching mock interaction has been registered for the current request");
            }

            if (matchingInteractions.Count() > 1)
            {
                throw new PactFailureException("More than one matching mock interaction has been registered for the current request");
            }

            return(matchingInteractions.Single());
        }