Example #1
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."));
        }
        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());
        }