Skip to content

Commit

Permalink
Fixed TestExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoergEg committed Mar 4, 2011
1 parent d72d758 commit c927246
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
12 changes: 7 additions & 5 deletions CQRSSample.Specs/AggregateRootTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// ReSharper disable InconsistentNaming
using System;
using System.Collections;
using System.Collections.Generic;
using CQRSSample.Domain;
using Castle.Core;
using CommonDomain;
using CQRSSample.Events;
using NUnit.Framework;

namespace CQRSSample.Specs
{
[TestFixture]
public abstract class AggregateRootTestFixture<T> where T : AggregateRoot, new()
public abstract class AggregateRootTestFixture<T> where T : IAggregate, new()
{
protected Exception Caught;
protected T Sut;
protected List<DomainEvent> Events;
protected ICollection Events;

protected abstract IEnumerable<DomainEvent> Given();
protected abstract void When();
Expand All @@ -21,12 +23,12 @@ namespace CQRSSample.Specs
public void Setup()
{
Sut = new T();
Sut.LoadFromHistory(Given());
Given().ForEach(x => Sut.ApplyEvent(x));

try
{
When();
Events = new List<DomainEvent>(Sut.GetChanges());
Events = Sut.GetUncommittedEvents();
}
catch (Exception ex)
{
Expand Down
4 changes: 4 additions & 0 deletions CQRSSample.Specs/CQRSSample.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=2.5.1.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL" />
<Reference Include="CommonDomain">
<HintPath>..\External Libs\CommonDomain\bin\CommonDomain.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\External Libs\NUnit\nunit.framework.dll</HintPath>
</Reference>
Expand Down
17 changes: 10 additions & 7 deletions CQRSSample.Specs/CommandTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Castle.Core;
using CommonDomain;
using CQRSSample.CommandHandlers;
using CQRSSample.Commands;
using CQRSSample.Domain;
Expand All @@ -12,13 +15,13 @@ namespace CQRSSample.Specs
public abstract class CommandTestFixture<TCommand, TCommandHandler, TAggregateRoot>
where TCommand : Command
where TCommandHandler : class, Handles<TCommand>
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregate, new()
{
protected TAggregateRoot AggregateRoot;
protected Handles<TCommand> CommandHandler;
protected Exception CaughtException;
protected IEnumerable<DomainEvent> PublishedEvents;
protected FakeRepository<TAggregateRoot> Repository;
protected ICollection PublishedEvents;
protected FakeRepository Repository;
protected virtual void SetupDependencies() { }

protected virtual IEnumerable<DomainEvent> Given()
Expand All @@ -33,20 +36,20 @@ protected virtual void Finally() { }
[SetUp]
public void Setup()
{
Repository = new FakeRepository<TAggregateRoot>();
Repository = new FakeRepository();
CaughtException = new ThereWasNoExceptionButOneWasExpectedException();
AggregateRoot = new TAggregateRoot();
AggregateRoot.LoadFromHistory(Given());
Given().ForEach(x => AggregateRoot.ApplyEvent(x));

CommandHandler = BuildCommandHandler();
SetupDependencies();
try
{
CommandHandler.Handle(When());
if (Repository.SavedAggregate == null)
PublishedEvents = AggregateRoot.GetChanges();
PublishedEvents = AggregateRoot.GetUncommittedEvents();
else
PublishedEvents = Repository.SavedAggregate.GetChanges();
PublishedEvents = Repository.SavedAggregate.GetUncommittedEvents();
}
catch (Exception exception)
{
Expand Down
16 changes: 9 additions & 7 deletions CQRSSample.Specs/FakeRepository.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System;
using CQRSSample.Domain;
using System.Collections.Generic;
using CommonDomain;
using CommonDomain.Persistence;

namespace CQRSSample.Specs
{
public class FakeRepository<T> : IRepository<T> where T : AggregateRoot, new()
public class FakeRepository : IRepository
{
public T SavedAggregate { get; set; }
public IAggregate SavedAggregate { get; set; }

public void Save(T aggregate, int expectedVersion)
public TAggregate GetById<TAggregate>(Guid id, int version) where TAggregate : class, IAggregate
{
SavedAggregate = aggregate;
return Activator.CreateInstance(typeof(TAggregate), id) as TAggregate;
}

public T GetById(Guid id)
public void Save(IAggregate aggregate, Guid commitId, Action<IDictionary<string, object>> updateHeaders)
{
return new T();
SavedAggregate = aggregate;
}
}
}
25 changes: 15 additions & 10 deletions CQRSSample.Specs/TestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections;
using CQRSSample.Events;
using System.Linq;
using NUnit.Framework;
Expand All @@ -8,14 +8,14 @@ namespace CQRSSample.Specs
{
public static class TestExtensions
{
public static DomainEvent Number(this IEnumerable<DomainEvent> events, int value)
public static DomainEvent Number(this ICollection events, int value)
{
return events.ToList()[--value];
return (DomainEvent)events.Cast<object>().ToList()[--value];
}

public static void CountIs(this IEnumerable<DomainEvent> events, int value)
public static void CountIs(this ICollection events, int value)
{
Assert.AreEqual(value, events.ToList().Count());
Assert.AreEqual(value, events.Count);
}

public static void WillBeOfType<TType>(this object theEvent)
Expand Down Expand Up @@ -48,14 +48,19 @@ public static void WithMessage(this Exception theException, string message)
Assert.AreEqual(message, theException.Message);
}

public static TDomainEvent Last<TDomainEvent>(this IEnumerable<DomainEvent> events) where TDomainEvent : DomainEvent
public static TDomainEvent Last<TDomainEvent>(this ICollection events) where TDomainEvent : DomainEvent
{
return (TDomainEvent)events.Last();
}

public static object LastMinus(this IEnumerable<DomainEvent> events, int minus)
}

public static object Last(this ICollection events)
{
return events.Cast<object>().Last();
}

public static object LastMinus(this ICollection events, int minus)
{
return events.ToList()[events.Count() - 1 - minus];
return events.Cast<object>().ToList()[events.Count - 1 - minus];
}

//public static TDomainEvent LastMinus<TDomainEvent>(this IEnumerable<DomainEvent> events, int minus)
Expand Down
8 changes: 5 additions & 3 deletions CQRSSample/CommandHandlers/CreateCustomerCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using CommonDomain.Persistence;
using CQRSSample.Commands;
using CQRSSample.Domain;

namespace CQRSSample.CommandHandlers
{
public class CreateCustomerCommandHandler : Handles<CreateCustomerCommand>
{
private readonly IRepository<Customer> _repository;
private readonly IRepository _repository;

public CreateCustomerCommandHandler(IRepository<Customer> repository)
public CreateCustomerCommandHandler(IRepository repository)
{
_repository = repository;
}
Expand All @@ -18,7 +20,7 @@ public void Handle(CreateCustomerCommand command)
new Address(command.Street, command.StreetNumber,
command.PostalCode, command.City),
new PhoneNumber(command.PhoneNumber));
_repository.Save(client, -1);
_repository.Save(client, Guid.NewGuid(), null);
}
}
}
7 changes: 5 additions & 2 deletions CQRSSample/CommandHandlers/CustomerCommandHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using CQRSSample.Commands;
using CQRSSample.Domain;
using CommonDomain.Persistence;
Expand All @@ -15,9 +16,11 @@ public RelocatingCustomerCommandHandler(IRepository repository)

public void Handle(RelocatingCustomerCommand command)
{
var customer = _repository.GetById<Customer>(command.Id, command.Version);
//TODO: Version
const int version = 0;
var customer = _repository.GetById<Customer>(command.Id, version);
customer.RelocateCustomer(command.Street, command.Streetnumber, command.PostalCode, command.City);
_repository.Save(customer, customer.Version);
_repository.Save(customer, Guid.NewGuid(), null);
}
}
}

0 comments on commit c927246

Please sign in to comment.