Skip to content

Instantly share code, notes, and snippets.

@NigelThorne
Created June 24, 2015 00:29
Show Gist options
  • Save NigelThorne/07b90502a8dcd172a4ba to your computer and use it in GitHub Desktop.
Save NigelThorne/07b90502a8dcd172a4ba to your computer and use it in GitHub Desktop.
An alternative mechanism for passing context to SpecFor
public static class ABus
{
// can be a simple method
public static void that_works(ISpecs<Config.Backend.Iguana.Services.IguanaResourceService> state)
{
state.SUT.Bus = state.GetMockFor<IBus>().Object;
}
}
public static class AMapper
{
// or a delegate if you need to pass parameters
public static Action<ISpecs<Config.Backend.Iguana.Services.IguanaResourceService>> that_works_for<TResource>(TResource resource)
{
return state =>
{
var resourcePostedToExternalSystemEvent = Builder<ResourcePostedToExternalSystemEvent<TResource>>.CreateNew()
.With(i => i.Resource = resource)
.Build();
state.GetMockFor<IMapper>().Setup<ResourcePostedToExternalSystemEvent<TResource>>(m =>
m.Map<TResource, ResourcePostedToExternalSystemEvent<TResource>>(It.IsAny<TResource>()))
.Returns(resourcePostedToExternalSystemEvent);
};
}
}
public class AnIguanaConnection
{
public static Action<ISpecs<Config.Backend.Iguana.Services.IguanaResourceService>> that_failing_with<TResource>(Exception exception)
{
return state =>
{
state.GetMockFor<IIguanaConnectionService>()
.Setup(a => a.PostAsJsonContent(It.IsAny<string>(), It.IsAny<TResource>(), 6544))
.Throws(exception);
};
}
}
public class PostEventToIguanaFails : ExtendedSpecsFor<Config.Backend.Iguana.Services.IguanaResourceService>
{
private IHubEvent _hubEvent;
private Exception _rootException;
protected SpecimenStatusUpdateResource _specimenStatusUpdateResource;
protected override void Given()
{
_specimenStatusUpdateResource = Builder<SpecimenStatusUpdateResource>.CreateNew().Build();
_rootException = new Exception("bang!");
Given( ABus.that_works );
Given( AMapper.that_works_for(_specimenStatusUpdateResource) );
Given( AnIguanaConnection.that_failing_with<SpecimenStatusUpdateResource>(_rootException) );
}
//...
}
// allow delegates/lamdas/functions to be used as contexts
public class DelegateContext<T> : IContext<T>
{
private readonly Action<ISpecs<T>> _method;
public DelegateContext(Action<ISpecs<T>> method)
{
_method = method;
}
public void Initialize(ISpecs<T> state)
{
_method(state);
}
}
// extend baseclass to add Given syntax that takes Action
public abstract class ExtendedSpecsFor<T> : SpecsFor<T> where T : class
{
protected void Given(Action<ISpecs<T>> context)
{
Given(new DelegateContext<T>(context));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment