Skip to content

Commit

Permalink
Simplified fixtures. fixes pjvds#75
Browse files Browse the repository at this point in the history
  • Loading branch information
jasondentler committed Mar 26, 2011
1 parent a489756 commit 2b2f467
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 215 deletions.
69 changes: 16 additions & 53 deletions Framework/src/Ncqrs.Spec/BigBangTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCommand>
: BaseTestFixture
: DomainTestFixture<TCommand>
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<IPublishableEvent> PublishedEvents { get; private set; }

protected abstract IEnumerable<object> GivenEvents();
protected abstract TCommand WhenExecuting();

protected override void Given()

protected ICommandService CommandService { get; private set; }

protected virtual IEnumerable<object> 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<ICommandService>();
cmdService.Execute(ExecutedCommand);
base.SetupDependencies();
GenerateEventSourceId();
RecordGivenEvents();
CommandService = NcqrsEnvironment.Get<ICommandService>();
}

protected override void Finally()
protected override void Execute(TCommand command)
{
PublishedEvents = _eventBus.GetPublishedEvents();
RevertConfiguration();
base.Finally();
CommandService.Execute(command);
}

private void GenerateEventSourceId()
Expand All @@ -58,8 +43,9 @@ private void GenerateEventSourceId()

private void RecordGivenEvents()
{
_eventStore = new EventStoreWrapper();
_eventStore.Given(ConvertGivenEvents());
var store = NcqrsEnvironment.Get<IEventStore>();
var givenEventStream = ConvertGivenEvents();
store.Store(givenEventStream);
}

private UncommittedEventStream ConvertGivenEvents()
Expand All @@ -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<IEventStore>(_eventStore);
_configuration.Register<IEventBus>(_eventBus);
RegisterFakesInConfiguration(_configuration);
_configuration.Push();
}

protected virtual void RegisterFakesInConfiguration(EnvironmentConfigurationWrapper configuration)
{
}

private void RevertConfiguration()
{
_configuration.Pop();
}

}
}
47 changes: 9 additions & 38 deletions Framework/src/Ncqrs.Spec/CommandTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCommand>
: DomainTestFixture<TCommand>
where TCommand : ICommand
{
protected Exception CaughtException{ get; private set; }

protected IEnumerable<UncommittedEvent> PublishedEvents{ get; private set;}

protected TCommand ExecutedCommand { get; private set; }

protected abstract TCommand WhenExecutingCommand();

protected virtual void SetupDependencies() { }
protected virtual void Finally() { }
protected ICommandExecutor<ICommand> 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<ICommand> BuildCommandExecutor();
Expand Down
58 changes: 58 additions & 0 deletions Framework/src/Ncqrs.Spec/DomainTestFixture.cs
Original file line number Diff line number Diff line change
@@ -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<TCommand>
: BaseTestFixture
where TCommand : ICommand
{

private EnvironmentConfigurationWrapper _configuration;

protected IEnumerable<UncommittedEvent> 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();
}

}
}
79 changes: 0 additions & 79 deletions Framework/src/Ncqrs.Spec/Fakes/EventStoreWrapper.cs

This file was deleted.

43 changes: 0 additions & 43 deletions Framework/src/Ncqrs.Spec/Fakes/RecordingEventBus.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Framework/src/Ncqrs.Spec/Ncqrs.Spec.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<Compile Include="AndAttribute.cs" />
<Compile Include="AutoMappedCommandTestFixture.cs" />
<Compile Include="BaseTestFixture.cs" />
<Compile Include="DomainTestFixture.cs" />
<Compile Include="JsonEventSerializationFixture.cs" />
<Compile Include="CommandTestFixture.cs" />
<Compile Include="BigBangTestFixture.cs" />
Expand All @@ -108,9 +109,7 @@
<Compile Include="OneEventTestFixture.cs" />
<Compile Include="EventContext.cs" />
<Compile Include="Fakes\EnvironmentConfigurationWrapper.cs" />
<Compile Include="Fakes\EventStoreWrapper.cs" />
<Compile Include="Fakes\FrozenClock.cs" />
<Compile Include="Fakes\RecordingEventBus.cs" />
<Compile Include="GivenAttribute.cs" />
<Compile Include="Prepare.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 2b2f467

Please sign in to comment.