コード例 #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.");
        }
コード例 #2
0
        public void GetMockInteractions_WithNoRegisteredInteractions_ReturnsEmptyList()
        {
            var context = new NancyContext();
            var result = context.GetMockInteractions();

            Assert.Empty(result);
        }
コード例 #3
0
        public void GetMockInteractions_WithRegisteredInteractions_ReturnsInteractions()
        {
            var interactions = new List<ProviderServiceInteraction>
            {
                new ProviderServiceInteraction { Request = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/events" }, Response = new ProviderServiceResponse()},
                new ProviderServiceInteraction { Request = new ProviderServiceRequest { Method = HttpVerb.Post, Path = "/events" }, Response = new ProviderServiceResponse()},
            };

            var context = new NancyContext();
            context.SetMockInteraction(interactions);

            var result = context.GetMockInteractions().ToList();

            Assert.Equal(2, result.Count());
            Assert.Equal(interactions.First(), result.First());
            Assert.Equal(interactions.Last(), result.Last());
        }