Created
February 6, 2011 00:22
-
-
Save chrisnicola/812958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nCQRS MSpec test helpers