diff --git a/Framework/src/Ncqrs.Spec/BigBangTestFixture.cs b/Framework/src/Ncqrs.Spec/BigBangTestFixture.cs index 609cf744..51831991 100644 --- a/Framework/src/Ncqrs.Spec/BigBangTestFixture.cs +++ b/Framework/src/Ncqrs.Spec/BigBangTestFixture.cs @@ -3,51 +3,36 @@ using Ncqrs.Commanding; using Ncqrs.Commanding.ServiceModel; using Ncqrs.Eventing; -using Ncqrs.Eventing.ServiceModel.Bus; using Ncqrs.Eventing.Storage; -using Ncqrs.Spec.Fakes; namespace Ncqrs.Spec { - [Specification] public abstract class BigBangTestFixture - : BaseTestFixture + : DomainTestFixture where TCommand : ICommand { - private EnvironmentConfigurationWrapper _configuration; - private EventStoreWrapper _eventStore; - private RecordingEventBus _eventBus; - protected Guid EventSourceId { get; private set; } - protected TCommand ExecutedCommand { get; private set; } - protected IEnumerable PublishedEvents { get; private set; } - - protected abstract IEnumerable GivenEvents(); - protected abstract TCommand WhenExecuting(); - - protected override void Given() + + protected ICommandService CommandService { get; private set; } + + protected virtual IEnumerable GivenEvents() { - base.Given(); - GenerateEventSourceId(); - RecordGivenEvents(); - SetupRecordingEventBus(); - ReconfigureEnvironment(); + return new object[0]; } - protected override void When() + protected override void SetupDependencies() { - ExecutedCommand = WhenExecuting(); - var cmdService = NcqrsEnvironment.Get(); - cmdService.Execute(ExecutedCommand); + base.SetupDependencies(); + GenerateEventSourceId(); + RecordGivenEvents(); + CommandService = NcqrsEnvironment.Get(); } - protected override void Finally() + protected override void Execute(TCommand command) { - PublishedEvents = _eventBus.GetPublishedEvents(); - RevertConfiguration(); - base.Finally(); + CommandService.Execute(command); } private void GenerateEventSourceId() @@ -58,8 +43,9 @@ private void GenerateEventSourceId() private void RecordGivenEvents() { - _eventStore = new EventStoreWrapper(); - _eventStore.Given(ConvertGivenEvents()); + var store = NcqrsEnvironment.Get(); + var givenEventStream = ConvertGivenEvents(); + store.Store(givenEventStream); } private UncommittedEventStream ConvertGivenEvents() @@ -69,28 +55,5 @@ private UncommittedEventStream ConvertGivenEvents() .ForSourceUncomitted(EventSourceId, Guid.NewGuid()); } - private void SetupRecordingEventBus() - { - _eventBus = new RecordingEventBus(); - } - - private void ReconfigureEnvironment() - { - _configuration = new EnvironmentConfigurationWrapper(); - _configuration.Register(_eventStore); - _configuration.Register(_eventBus); - RegisterFakesInConfiguration(_configuration); - _configuration.Push(); - } - - protected virtual void RegisterFakesInConfiguration(EnvironmentConfigurationWrapper configuration) - { - } - - private void RevertConfiguration() - { - _configuration.Pop(); - } - } } diff --git a/Framework/src/Ncqrs.Spec/CommandTestFixture.cs b/Framework/src/Ncqrs.Spec/CommandTestFixture.cs index 48d7ca68..04498a42 100644 --- a/Framework/src/Ncqrs.Spec/CommandTestFixture.cs +++ b/Framework/src/Ncqrs.Spec/CommandTestFixture.cs @@ -7,51 +7,22 @@ namespace Ncqrs.Spec { - [Specification] - [TestFixture] // TODO: Testdriven.net debug runner doesn't recognize inhiret attributes. Use native for now. + public abstract class CommandTestFixture + : DomainTestFixture where TCommand : ICommand { - protected Exception CaughtException{ get; private set; } - - protected IEnumerable PublishedEvents{ get; private set;} - protected TCommand ExecutedCommand { get; private set; } - - protected abstract TCommand WhenExecutingCommand(); - - protected virtual void SetupDependencies() { } - protected virtual void Finally() { } + protected ICommandExecutor CommandExecutor { get; private set; } - [Given] - [SetUp] // TODO: Testdriven.net debug runner doesn't recognize inhiret attributes. Use native for now. - public void Setup() + protected override void SetupDependencies() { - var commandExecutor = BuildCommandExecutor(); - PublishedEvents = new UncommittedEvent[0]; - - SetupDependencies(); - try - { - var command = WhenExecutingCommand(); - - using (var context = new EventContext()) - { - commandExecutor.Execute(command); - - ExecutedCommand = command; + CommandExecutor = BuildCommandExecutor(); + } - PublishedEvents = context.Events; - } - } - catch (Exception exception) - { - CaughtException = exception; - } - finally - { - Finally(); - } + protected override void Execute(TCommand command) + { + CommandExecutor.Execute(command); } protected abstract ICommandExecutor BuildCommandExecutor(); diff --git a/Framework/src/Ncqrs.Spec/DomainTestFixture.cs b/Framework/src/Ncqrs.Spec/DomainTestFixture.cs new file mode 100644 index 00000000..7fbe1893 --- /dev/null +++ b/Framework/src/Ncqrs.Spec/DomainTestFixture.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using Ncqrs.Commanding; +using Ncqrs.Eventing; +using Ncqrs.Spec.Fakes; + +namespace Ncqrs.Spec +{ + + [Specification] + public abstract class DomainTestFixture + : BaseTestFixture + where TCommand : ICommand + { + + private EnvironmentConfigurationWrapper _configuration; + + protected IEnumerable PublishedEvents { get; private set; } + + protected TCommand ExecutedCommand { get; private set; } + + protected abstract TCommand WhenExecuting(); + + protected abstract void Execute(TCommand command); + + protected virtual void SetupDependencies() + { + _configuration = new EnvironmentConfigurationWrapper(); + RegisterFakesInConfiguration(_configuration); + _configuration.Push(); + } + + protected virtual void RegisterFakesInConfiguration(EnvironmentConfigurationWrapper configuration) + { + } + + protected override void Given() + { + SetupDependencies(); + PublishedEvents = new UncommittedEvent[0]; + ExecutedCommand = WhenExecuting(); + } + + protected override void When() + { + using (var ctx = new EventContext()) + { + Execute(ExecutedCommand); + PublishedEvents = ctx.Events; + } + } + + protected override void Finally() + { + _configuration.Pop(); + } + + } +} diff --git a/Framework/src/Ncqrs.Spec/Fakes/EventStoreWrapper.cs b/Framework/src/Ncqrs.Spec/Fakes/EventStoreWrapper.cs deleted file mode 100644 index 10f6942f..00000000 --- a/Framework/src/Ncqrs.Spec/Fakes/EventStoreWrapper.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Ncqrs.Eventing; -using Ncqrs.Eventing.Storage; - -namespace Ncqrs.Spec.Fakes -{ - - public class EventStoreWrapper : IEventStore - { - - private readonly IEventStore _realStore; - private IEventStore _testStore; - private readonly HashSet _underTest; - - public EventStoreWrapper() - : this(NcqrsEnvironment.Get()) - { - } - - public EventStoreWrapper(IEventStore realStore) - { - _realStore = realStore; - _underTest = new HashSet(); - _testStore = new InMemoryEventStore(); - } - - public CommittedEventStream ReadFrom(Guid id, long minVersion, long maxVersion) - { - return _underTest.Contains(id) - ? _testStore.ReadFrom(id, minVersion, maxVersion) - : _realStore.ReadFrom(id, minVersion, maxVersion); - } - - public void Store(UncommittedEventStream eventStream) - { - Store(eventStream.CommitId, eventStream); - } - - private void Store(Guid commitId, IEnumerable events) - { - var eventsForTestStore = events.Where(e => _underTest.Contains(e.EventSourceId)); - var eventsForRealStore = events.Except(eventsForTestStore); - - if (eventsForTestStore.Any()) - _testStore.Store(BuildStream(commitId, eventsForTestStore)); - - if (eventsForRealStore.Any()) - _realStore.Store(BuildStream(commitId, eventsForRealStore)); - } - - private UncommittedEventStream BuildStream(Guid commitId, IEnumerable events) - { - var stream = new UncommittedEventStream(commitId); - foreach (var evnt in events) - stream.Append(evnt); - return stream; - } - - public void Given(UncommittedEventStream history) - { - var historyBySources = history.GroupBy(e => e.EventSourceId); - foreach (var sourceHistory in historyBySources) - { - _underTest.Add(sourceHistory.Key); - Store(history.CommitId, sourceHistory); - } - } - - public void Clear() - { - _testStore = new InMemoryEventStore(); - _underTest.Clear(); - } - - } -} - diff --git a/Framework/src/Ncqrs.Spec/Fakes/RecordingEventBus.cs b/Framework/src/Ncqrs.Spec/Fakes/RecordingEventBus.cs deleted file mode 100644 index 5a0f433d..00000000 --- a/Framework/src/Ncqrs.Spec/Fakes/RecordingEventBus.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Ncqrs.Eventing.ServiceModel.Bus; - -namespace Ncqrs.Spec.Fakes -{ - public class RecordingEventBus : IEventBus - { - private readonly IEventBus _realBus; - private readonly List _recording; - - public RecordingEventBus() - : this(NcqrsEnvironment.Get()) - { - } - - public RecordingEventBus(IEventBus realBus) - { - _realBus = realBus; - _recording = new List(); - } - - public void Publish(IPublishableEvent eventMessage) - { - _recording.Add(eventMessage); - _realBus.Publish(eventMessage); - } - - public void Publish(IEnumerable eventMessages) - { - var messages = eventMessages.ToArray(); - _recording.AddRange(messages); - _realBus.Publish(messages); - } - - public IEnumerable GetPublishedEvents() - { - return _recording.ToArray(); - } - - - } -} diff --git a/Framework/src/Ncqrs.Spec/Ncqrs.Spec.csproj b/Framework/src/Ncqrs.Spec/Ncqrs.Spec.csproj index dadf4443..2bf3e947 100644 --- a/Framework/src/Ncqrs.Spec/Ncqrs.Spec.csproj +++ b/Framework/src/Ncqrs.Spec/Ncqrs.Spec.csproj @@ -100,6 +100,7 @@ + @@ -108,9 +109,7 @@ - -