Skip to content

Instantly share code, notes, and snippets.

@marcofranssen
Created August 10, 2011 17:56
Show Gist options
  • Save marcofranssen/1137631 to your computer and use it in GitHub Desktop.
Save marcofranssen/1137631 to your computer and use it in GitHub Desktop.
Gists related to my "Pitfall in FakeItEasy" blogpost on http://marcofranssen.nl
[Then]
public void it_should_throw_an_formatted_participant_nr_should_be_unique_exception_containing_the_formatted_participant_nr()
{
var exception = (FormattedParticipantNrShouldBeUniqueException)CaughtException;
exception.FormattedParticipantNr.Should().Be(_formattedParticipantNr);
}
protected override void RegisterFakesInConfiguration(EnvironmentConfigurationWrapper configuration)
{
var commandService = new CommandService();
commandService.RegisterExecutor(new EditParticipantCommandExecutor());
var uniqueParticipantGuard = A.Fake<IUniqueParticipantNrGuard>();
A.CallTo(uniqueParticipantGuard).DoesNothing().Once();
A.CallTo(uniqueParticipantGuard).Throws(
new FormattedParticipantNrShouldBeUniqueException(_formattedParticipantNr));
configuration.Register<ICommandService>(commandService);
configuration.Register(uniqueParticipantGuard);
//other fakes left for convenience
base.RegisterFakesInConfiguration(configuration);
}
public static class New
{
public static ScientificStudy ScientificStudy(string nr = "M001", string name = "Maastricht Study", string participantNrFormat = "P{0}", string boxNameFormat = "B{0}")
{
return new ScientificStudy(Default.Id.For.ScientificStudy, nr, name, participantNrFormat, boxNameFormat);
}
public static Participant Participant(ScientificStudy scientificStudy, string participantNrFormat = "P{0}", int participantNr = 1, int gender = 0, string birthdate = "19861117")
{
var realGender = new Gender(gender);
var realBirthDate = new PartialDate(birthdate);
return new Participant(Default.Id.For.Participant, scientificStudy, participantNrFormat, participantNr, realGender, realBirthDate);
}
//other members left for convenience
}
public class Participant : BisAggregateRoot
{
private readonly IUniqueParticipantNrGuard _uniqueFormattedParticipantNrVerifier = NcqrsEnvironment.Get<IUniqueParticipantNrGuard>();
//other members left for convenience
private string _participantNrFormat;
private int _participantNr;
private Gender _gender;
private PartialDate _birthDate;
private string _remark;
public Guid ParticipantId { get { return EventSourceId; } }
// ReSharper disable UnusedMember.Local
private Participant()
// ReSharper restore UnusedMember.Local
{
}
public Participant(Guid participantId, ScientificStudy scientificStudy, string participantNrFormat, int participantNr, Gender gender, PartialDate birthdate)
: base(participantId)
{
_uniqueFormattedParticipantNrVerifier.GuardIsUnique(scientificStudy.ScientificStudyId, participantId,
string.Format(participantNrFormat, participantNr));
ApplyEvent(new ParticipantAddedEvent(Credentials)
{
ParticipantId = participantId,
ParticipantNrFormat = participantNrFormat,
ParticipantNr = participantNr,
ScientificStudyId = scientificStudy.ScientificStudyId,
Gender = gender.ActualValue,
Birthdate = birthdate.ActualValue
});
}
public void Edit(ScientificStudy scientificStudy, string participantNrFormat, int participantNr, Gender gender, PartialDate birthdate, string remark)
{
//Some other business rules
_uniqueFormattedParticipantNrVerifier.GuardIsUnique(scientificStudy.ScientificStudyId, ParticipantId,
string.Format(participantNrFormat, participantNr));
//Some other business rules
ApplyEvent(new ParticipantEditedEvent(Credentials)
{
ParticipantId = ParticipantId,
ParticipantNrFormat = participantNrFormat,
ParticipantNr = participantNr,
Gender = gender.ActualValue,
Birthdate = birthdate.ActualValue
});
}
protected void OnParticipantAdded(ParticipantAddedEvent e)
{
_claimedAliquots = new List<ClaimedAliquot>();
_participantNrFormat = e.ParticipantNrFormat;
_participantNr = e.ParticipantNr;
_gender = new Gender(e.Gender);
_birthDate = new PartialDate(e.Birthdate);
}
protected void OnParticipantEdited(ParticipantEditedEvent e)
{
_participantNrFormat = e.ParticipantNrFormat;
_participantNr = e.ParticipantNr;
_gender = new Gender(e.Gender);
_birthDate = new PartialDate(e.Birthdate);
_remark = e.Remark;
}
}
protected override void SetupDependencies()
{
base.SetupDependencies();
var eventStore = NcqrsEnvironment.Get<IEventStore>();
var scientificStudy = New.ScientificStudy();
var scientificStudyEvent = scientificStudy.TrackChanges().GetChanges();
var participantEvents = New.Participant(scientificStudy).GetChanges();
eventStore.Store(Prepare.Events(scientificStudyEvent).ForSourceUncomitted(Default.Id.For.ScientificStudy, Default.Id.Random()));
eventStore.Store(Prepare.Events(participantEvents).ForSourceUncomitted(Default.Id.For.Participant, Default.Id.Random()));
}
protected override void RegisterFakesInConfiguration(EnvironmentConfigurationWrapper configuration)
{
var commandService = new CommandService();
commandService.RegisterExecutor(new EditParticipantCommandExecutor());
var uniqueParticipantGuard = A.Fake<IUniqueParticipantNrGuard>();
A.CallTo(uniqueParticipantGuard).Throws(
new FormattedParticipantNrShouldBeUniqueException(_formattedParticipantNr));
A.CallTo(uniqueParticipantGuard).DoesNothing().Once();
configuration.Register<ICommandService>(commandService);
configuration.Register(uniqueParticipantGuard);
//other fakes left for convenience
base.RegisterFakesInConfiguration(configuration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment