Skip to content

Commit

Permalink
Changing setUp and tearDown actions to be invoked before and after ea…
Browse files Browse the repository at this point in the history
…ch provider interaction verify, instead of at the start and end of the provider interaction verify process.
  • Loading branch information
neilcampbell committed Mar 5, 2015
1 parent 4f4e38b commit 0e0f669
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ public void Validate_WithNoInteractionsAndProviderStatesSetUpDefined_SetUpAction
}

[Fact]
public void Validate_ProviderStatesSetUpDefined_SetUpActionIsInvoked()
public void Validate_ProviderStatesSetUpDefined_SetUpActionIsInvokedForEachInteraction()
{
var actionInkoved = false;
var actionInvocationCount = 0;
var pact = new ProviderServicePactFile
{
Consumer = new Party { Name = "My client" },
Expand All @@ -259,22 +259,26 @@ public void Validate_ProviderStatesSetUpDefined_SetUpActionIsInvoked()
new ProviderServiceInteraction
{
Description = "My interaction"
},
new ProviderServiceInteraction
{
Description = "My interaction 2"
}
}
};
var providerStates = new ProviderStates(setUp: () => { actionInkoved = true; }, tearDown: null);
var providerStates = new ProviderStates(setUp: () => { actionInvocationCount++; }, tearDown: null);

var validator = GetSubject();

validator.Validate(pact, providerStates);

Assert.True(actionInkoved, "Provider states pact setUp action is invoked");
Assert.Equal(pact.Interactions.Count(), actionInvocationCount);
}

[Fact]
public void Validate_ProviderStatesTearDownDefined_TearDownActionIsInvoked()
public void Validate_ProviderStatesTearDownDefined_TearDownActionIsInvokedForEachInteraction()
{
var actionInkoved = false;
var actionInvocationCount = 0;
var pact = new ProviderServicePactFile
{
Consumer = new Party { Name = "My client" },
Expand All @@ -284,16 +288,20 @@ public void Validate_ProviderStatesTearDownDefined_TearDownActionIsInvoked()
new ProviderServiceInteraction
{
Description = "My interaction"
},
new ProviderServiceInteraction
{
Description = "My interaction 2"
}
}
};
var providerStates = new ProviderStates(setUp: null, tearDown: () => { actionInkoved = true; });
var providerStates = new ProviderStates(setUp: null, tearDown: () => { actionInvocationCount++; });

var validator = GetSubject();

validator.Validate(pact, providerStates);

Assert.True(actionInkoved, "Provider states pact tearDown action is invoked");
Assert.Equal(pact.Interactions.Count(), actionInvocationCount);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,54 +51,48 @@ public void Validate(ProviderServicePactFile pactFile, ProviderStates providerSt

if (pactFile.Interactions != null && pactFile.Interactions.Any())
{
InvokePactSetUpIfApplicable(providerStates);

var interationNumber = 1;
try //TODO: Clean this up once the validators/comparers no longer throw
foreach (var interaction in pactFile.Interactions)
{
foreach (var interaction in pactFile.Interactions)
{
ProviderState providerStateItem = null;

if (interaction.ProviderState != null)
{
try
{
providerStateItem = providerStates.Find(interaction.ProviderState);
}
catch (Exception)
{
providerStateItem = null;
}

if (providerStateItem == null)
{
throw new InvalidOperationException(String.Format("providerState '{0}' was defined by a consumer, however could not be found. Please supply this provider state.", interaction.ProviderState));
}
}

InvokeInteractionSetUpIfApplicable(providerStateItem);
InvokePactSetUpIfApplicable(providerStates);

_reporter.ReportInfo(String.Format("{0}) Verifying a Pact between {1} and {2} - {3}.", interationNumber, pactFile.Consumer.Name, pactFile.Provider.Name, interaction.Description));
ProviderState providerStateItem = null;

if (interaction.ProviderState != null)
{
try
{
ValidateInteraction(interaction);
providerStateItem = providerStates.Find(interaction.ProviderState);
}
finally
catch (Exception)
{
InvokeInteractionTearDownIfApplicable(providerStateItem);
providerStateItem = null;
}

if (providerStateItem == null)
{
throw new InvalidOperationException(String.Format("providerState '{0}' was defined by a consumer, however could not be found. Please supply this provider state.", interaction.ProviderState));
}

interationNumber++;
}

_reporter.ThrowIfAnyErrors();
}
finally
{
InvokeTearDownIfApplicable(providerStates);
InvokeProviderStateSetUpIfApplicable(providerStateItem);

_reporter.ReportInfo(String.Format("{0}) Verifying a Pact between {1} and {2} - {3}.", interationNumber, pactFile.Consumer.Name, pactFile.Provider.Name, interaction.Description));

try
{
ValidateInteraction(interaction);
}
finally
{
InvokeProviderStateTearDownIfApplicable(providerStateItem);
InvokeTearDownIfApplicable(providerStates);
}

interationNumber++;
}

_reporter.ThrowIfAnyErrors();
}
}

Expand Down Expand Up @@ -127,15 +121,15 @@ private void InvokeTearDownIfApplicable(ProviderStates providerStates)
}
}

private void InvokeInteractionSetUpIfApplicable(ProviderState providerState)
private void InvokeProviderStateSetUpIfApplicable(ProviderState providerState)
{
if (providerState != null && providerState.SetUp != null)
{
providerState.SetUp();
}
}

private void InvokeInteractionTearDownIfApplicable(ProviderState providerState)
private void InvokeProviderStateTearDownIfApplicable(ProviderState providerState)
{
if (providerState != null && providerState.TearDown != null)
{
Expand Down

0 comments on commit 0e0f669

Please sign in to comment.