Skip to content

Instantly share code, notes, and snippets.

@chrisnicola
Created February 6, 2011 00:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisnicola/812958 to your computer and use it in GitHub Desktop.
Save chrisnicola/812958 to your computer and use it in GitHub Desktop.
public static class DomainSpecificationExtensions
{
public static void ShouldHave<T>(this IEnumerable<ISourcedEvent> source, params Predicate<T>[] conditions) where T : class, ISourcedEvent
{
T actualEvent = (T) source.FirstOrDefault(x => x is T);
if (actualEvent == null)
throw new SpecificationException(string.Format("{0} did not happen as expected", typeof (T).Name));
actualEvent.ShouldMatch(conditions);
}
public static void ShouldMatch<T>(this T actual, params Predicate<T>[] conditions) {
foreach (var condition in conditions)
{
if (!condition.Invoke(actual))
throw new SpecificationException(string.Format("{0} did not match the condition {1}", typeof(T).Name,
condition.Method.GetMethodBody()));
}
}
}
/// <summary>
/// Base conext for nCQRS BDD tests. Provides Given() and When() context setup methods. The setup is as follows:
/// Given({An event history})
/// When({A command of TCommand to execute})
/// ExpectedEvents = {The exact number of events to expect will happen}
/// </summary>
/// <typeparam name="TCommand">The type of command that will excuted when passed to When()</typeparam>
/// <typeparam name="TCommandExecutor">The command executor implementation that will be used to execute the command</typeparam>
public class domain_context<TCommand, TCommandExecutor> where TCommandExecutor : XQCommandExecutor<TCommand>, new() where TCommand : ICommand
{
static IUnitOfWorkContext _mockUoW;
static TCommandExecutor _commandExecutor;
protected static TCommand Command;
protected static int ExpectedEvents;
protected static IList<ISourcedEvent> Events;
static Action<AggregateRoot, ISourcedEvent> _callback;
Establish context = () =>
{
//The callback tracks the events that occur during the test and adds them to the Events list
Events = new List<ISourcedEvent>();
_callback = (x,e) => Events.Add(e);
AggregateRoot.RegisterThreadStaticEventAppliedCallback(_callback);
//The UoW is how the CommandExecutor loads existing aggregate roots to act upon so we create a mock
var mockUoWFactory = A.Fake<IUnitOfWorkFactory>();
_mockUoW = A.Fake<IUnitOfWorkContext>();
A.CallTo(() => mockUoWFactory.CreateUnitOfWork()).Returns(_mockUoW);
//Register the mock to the nCQRS configuration
NcqrsEnvironment.SetDefault(mockUoWFactory);
_commandExecutor = new TCommandExecutor();
};
Cleanup after = () => AggregateRoot.UnregisterThreadStaticEventAppliedCallback(_callback);
protected static T Given<T>(params SourcedEvent[] history) where T : AggregateRoot
{
var aggregateRoot = new SimpleAggregateRootCreationStrategy().CreateAggregateRoot<T>();
var preparedHistory = Prepare.Events(history).ForSource(aggregateRoot.EventSourceId);
aggregateRoot.InitializeFromHistory(preparedHistory);
A.CallTo(() => _mockUoW.GetById<T>(aggregateRoot.EventSourceId)).Returns(aggregateRoot);
return aggregateRoot;
}
protected static void When(TCommand command)
{
Command = command;
_commandExecutor.Execute(command);
}
}
@chrisnicola
Copy link
Author

nCQRS MSpec test helpers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment