Skip to content

Instantly share code, notes, and snippets.

@JamesTryand
Created November 5, 2012 11:19
Show Gist options
  • Select an option

  • Save JamesTryand/4016724 to your computer and use it in GitHub Desktop.

Select an option

Save JamesTryand/4016724 to your computer and use it in GitHub Desktop.
Simple.Testing with TransformedSpecification for testing against events.
namespace EventsUnitTests
{
public class SimpleReadSideTestWithCustomPriorEvents
{
private Guid AggregateId;
public Specification Test()
{
return new TransformedSpecification<Given<Enquiry>, Enquiry, IEnumerable<Event>>()
{
Before = () =>
{
AggregateId = Guid.NewGuid();
},
On = () => new Given<Enquiry>(new Event[] {
new EnquiryCreatedEvent { Id = AggregateId, Version = 1 }, // Right now we are not using 'builders', but it is likely that we would be wanting to have builder classes to make this more fluent.
new BrandAssignedEvent{ Id = AggregateId, Version = 2, BrandId = "fancy" },
new CommissionAssignedToEnquiryEvent{Id = AggregateId, Version = 3, Commission = new Some.Commission.SomeCommission() { DayFee = 50000, MonthFee = 1000, WeekFee = 1500 } },
}),
When = given => given.ApplyCommand(sut => sut.UpdateElement("Postcode", "CH3 9SF")),
AndTransformedBy = when => when.GetUncommittedChanges(),
Expect =
{
it => it != null,
it => (First<Event>(it)) is Event,
it => Last<EnquiryElementChangedEvent<string>>(it) is EnquiryElementChangedEvent<string>,
it => it.Last() is EnquiryElementChangedEvent<string>,
it => ((EnquiryElementChangedEvent<string>)it.Last()).ElementName.Equals("Postcode")
},
};
}
public T First<T>(IEnumerable<Event> t) where T : Event
{
return t.ToList()[0] as T;
}
public T Last<T>(IEnumerable<Event> t) where T : Event
{
return t.ToList().Last() as T;
}
}
public class Given<T> where T : AggregateRoot, new()
{
public Guid AggregateId;
public T sut; // Subject Under Test
public Given(IEnumerable<Event> priorEvents)
{
var p = priorEvents;
AggregateId = p.First().AsDynamic().Id;
// sut = Activator.CreateInstance(typeof(T), new[] { AggregateId }) as T;
sut = Activator.CreateInstance(typeof(T),
BindingFlags.CreateInstance |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.OptionalParamBinding,
// null, new Object[] { Type.Missing }, null) as T;
null, new Object[] { AggregateId }, null) as T;
sut.LoadsFromHistory(p.ToArray());
}
public Given(IRepository<T> repository, IEnumerable<Event> priorEvents)
: this(priorEvents)
{
repository.Save(sut, 0);
}
public T ApplyCommand(Action<T> command)
{
command(sut);
return sut;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment