Skip to content

Commit

Permalink
Adding a couple more logging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell committed Apr 19, 2015
1 parent f64289d commit f214409
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,24 @@ public void Handle_WithAGetRequestToInteractionsVerificationAndRegisteredInterac
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}

[Fact]
public void Handle_WithAGetRequestToInteractionsVerificationAndRegisteredInteractionWasNotCalled_LogsTheMissingRequest()
{
var context = new NancyContext
{
Request = new Request("GET", "/interactions/verification", "http")
};

var handler = GetSubject();


_mockProviderRepository.TestScopedInteractions.Returns(new List<ProviderServiceInteraction> { new ProviderServiceInteraction() });

var response = handler.Handle(context);

_mockLog.Received().Log(LogLevel.Error, Arg.Any<Func<string>>(), null, Arg.Any<object[]>());
}

[Fact]
public void Handle_WithAGetRequestToInteractionsVerificationAndRegisteredInteractionWasCalledMultipleTimes_ReturnsInternalServerErrorResponse()
{
Expand Down Expand Up @@ -379,6 +397,34 @@ public void Handle_WithAGetRequestToInteractionsVerificationAndRegisteredInterac
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}

[Fact]
public void Handle_WithAGetRequestToInteractionsVerificationAndRegisteredInteractionWasCalledMultipleTimes_LogsTheError()
{
var context = new NancyContext
{
Request = new Request("GET", "/interactions/verification", "http")
};

var interactions = new List<ProviderServiceInteraction>
{
new ProviderServiceInteraction()
};

var handler = GetSubject();

_mockProviderRepository.TestScopedInteractions.Returns(interactions);

_mockProviderRepository.HandledRequests.Returns(new List<HandledRequest>
{
new HandledRequest(new ProviderServiceRequest(), interactions.First()),
new HandledRequest(new ProviderServiceRequest(), interactions.First())
});

var response = handler.Handle(context);

_mockLog.Received().Log(LogLevel.Error, Arg.Any<Func<string>>(), null, Arg.Any<object[]>());
}

[Fact]
public void Handle_WithAGetRequestToInteractionsVerificationAndNoInteractionsRegisteredHoweverMockProviderRecievedInteractions_ReturnsInternalServerErrorResponse()
{
Expand Down Expand Up @@ -482,6 +528,42 @@ public void Handle_WithAGetRequestToInteractionsVerificationAndAnInteractionWasS
Assert.Equal(failure, content);
}

[Fact]
public void Handle_WithAGetRequestToInteractionsVerificationAndAnInteractionWasSentButNotRegisteredByTheTest_LogsTheUnexpectedRequest()
{
const string failure = "An unexpected request POST /tester was seen by the mock provider service.";
var context = new NancyContext
{
Request = new Request("GET", "/interactions/verification", "http")
};

var handler = GetSubject();

var handledRequest = new ProviderServiceRequest();
var handledInteraction = new ProviderServiceInteraction { Request = handledRequest };

var unExpectedRequest = new ProviderServiceRequest { Method = HttpVerb.Post, Path = "/tester" };

_mockProviderRepository.TestScopedInteractions
.Returns(new List<ProviderServiceInteraction>
{
handledInteraction
});

_mockProviderRepository.HandledRequests
.Returns(new List<HandledRequest>
{
new HandledRequest(handledRequest, handledInteraction),
new HandledRequest(unExpectedRequest, null)
});

var response = handler.Handle(context);

var content = ReadResponseContent(response);

_mockLog.Received().Log(LogLevel.Error, Arg.Any<Func<string>>(), null, Arg.Any<object[]>());
}

[Fact]
public void Handle_WithAGetRequestToInteractionsVerificationAndAFailureOcurrs_ReturnsFailureResponseContent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ private IMockProviderRequestHandler GetSubject()
_mockProviderRepository = Substitute.For<IMockProviderRepository>();
_mockLog = Substitute.For<ILog>();

_mockLog.Log(Arg.Any<LogLevel>(), Arg.Any<Func<string>>(), Arg.Any<Exception>(), Arg.Any<object[]>())
.Returns(true);

return new MockProviderRequestHandler(_mockRequestMapper, _mockResponseMapper, _mockProviderRepository, _mockLog);
}

Expand Down Expand Up @@ -180,7 +183,7 @@ public void Handle_WhenExceptionIsThrownHandlingRequest_PactFailureExceptionIsTh
}

[Fact]
public void Handle_WhenGetMatchingMockInteractionThrows_RequestIsMarkedAsHandled()
public void Handle_WhenNoMatchingInteractionsAreFound_RequestIsMarkedAsHandled()
{
const string exceptionMessage = "No matching mock interaction has been registered for the current request";
var expectedRequest = new ProviderServiceRequest
Expand Down Expand Up @@ -221,7 +224,48 @@ public void Handle_WhenGetMatchingMockInteractionThrows_RequestIsMarkedAsHandled
}

[Fact]
public void Handle_WhenGetMatchingMockInteractionThrows_PactFailureExceptionIsThrown()
public void Handle_WhenNoMatchingInteractionsAreFound_ErrorIsLogged()
{
const string exceptionMessage = "No matching mock interaction has been registered for the current request";
var expectedRequest = new ProviderServiceRequest
{
Method = HttpVerb.Get,
Path = "/Test"
};
var nancyContext = new NancyContext
{
Request = new Request("GET", "/Test", "HTTP")
};

var handler = GetSubject();

_mockRequestMapper
.Convert(nancyContext.Request)
.Returns(expectedRequest);

_mockResponseMapper.Convert(Arg.Any<ProviderServiceResponse>())
.Returns(new Response
{
StatusCode = HttpStatusCode.InternalServerError
});

_mockProviderRepository
.When(x => x.GetMatchingTestScopedInteraction(expectedRequest))
.Do(x => { throw new PactFailureException(exceptionMessage); });

try
{
handler.Handle(nancyContext);
}
catch (Exception)
{
}

_mockLog.Received().Log(LogLevel.Error, Arg.Any<Func<string>>(), null, Arg.Any<object[]>());
}

[Fact]
public void Handle_WhenNoMatchingInteractionsAreFound_PactFailureExceptionIsThrown()
{
const string exceptionMessage = "No matching mock interaction has been registered for the current request";
var expectedRequest = new ProviderServiceRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ private Response HandlePostInteractionsRequest(NancyContext context)

private Response HandleGetInteractionsVerificationRequest()
{
//TODO: add logs for this! Trello has the example
//TODO: add the test name, so that we know what test

var registeredInteractions = _mockProviderRepository.TestScopedInteractions;

var comparisonResult = new ComparisonResult();
Expand Down
21 changes: 0 additions & 21 deletions Samples/EventApi/Consumer.Tests/EventsApiConsumerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,6 @@ public void CreateEvent_WhenCalledWithEvent_Succeeds()
Status = 201
});

_mockProviderService.UponReceiving("a request to create a new event 2")
.With(new ProviderServiceRequest
{
Method = HttpVerb.Post,
Path = "/events2",
Headers = new Dictionary<string, string>
{
{ "Content-Type", "application/json; charset=utf-8" }
},
Body = new
{
eventId,
timestamp = dateTime.ToString("O"),
eventType = "DetailsView"
}
})
.WillRespondWith(new ProviderServiceResponse
{
Status = 201
});

var consumer = new EventsApiClient(_mockProviderServiceBaseUri);

//Act / Assert
Expand Down

0 comments on commit f214409

Please sign in to comment.