Zacząłem używać AutoFixture http://autofixture.codeplex.com/, ponieważ moje testy jednostkowe były przepełnione wieloma ustawieniami danych. Spędzałem więcej czasu na ustawianiu danych niż na pisaniu testu jednostkowego. Oto przykład tego, jak mój pierwszy test jednostka wygląda (przykład pobranych z próbki aplikacji cargo z DDD blue book)Refaktoryzacja AutoFixture
[Test]
public void should_create_instance_with_correct_ctor_parameters()
{
var carrierMovements = new List<CarrierMovement>();
var deparureUnLocode1 = new UnLocode("AB44D");
var departureLocation1 = new Location(deparureUnLocode1, "HAMBOURG");
var arrivalUnLocode1 = new UnLocode("XX44D");
var arrivalLocation1 = new Location(arrivalUnLocode1, "TUNIS");
var departureDate1 = new DateTime(2010, 3, 15);
var arrivalDate1 = new DateTime(2010, 5, 12);
var carrierMovement1 = new CarrierMovement(departureLocation1, arrivalLocation1, departureDate1, arrivalDate1);
var deparureUnLocode2 = new UnLocode("CXRET");
var departureLocation2 = new Location(deparureUnLocode2, "GDANSK");
var arrivalUnLocode2 = new UnLocode("ZEZD4");
var arrivalLocation2 = new Location(arrivalUnLocode2, "LE HAVRE");
var departureDate2 = new DateTime(2010, 3, 18);
var arrivalDate2 = new DateTime(2010, 3, 31);
var carrierMovement2 = new CarrierMovement(departureLocation2, arrivalLocation2, departureDate2, arrivalDate2);
carrierMovements.Add(carrierMovement1);
carrierMovements.Add(carrierMovement2);
new Schedule(carrierMovements).ShouldNotBeNull();
}
Oto jak próbowałem byłaby to AutoFixture
[Test]
public void should_create_instance_with_correct_ctor_parameters_AutoFixture()
{
var fixture = new Fixture();
fixture.Register(() => new UnLocode(UnLocodeString()));
var departureLoc = fixture.CreateAnonymous<Location>();
var arrivalLoc = fixture.CreateAnonymous<Location>();
var departureDateTime = fixture.CreateAnonymous<DateTime>();
var arrivalDateTime = fixture.CreateAnonymous<DateTime>();
fixture.Register<Location, Location, DateTime, DateTime, CarrierMovement>(
(departure, arrival, departureTime, arrivalTime) => new CarrierMovement(departureLoc, arrivalLoc, departureDateTime, arrivalDateTime));
var carrierMovements = fixture.CreateMany<CarrierMovement>(50).ToList();
fixture.Register<List<CarrierMovement>, Schedule>((carrierM) => new Schedule(carrierMovements));
var schedule = fixture.CreateAnonymous<Schedule>();
schedule.ShouldNotBeNull();
}
private static string UnLocodeString()
{
var stringBuilder = new StringBuilder();
for (int i = 0; i < 5; i++)
stringBuilder.Append(GetRandomUpperCaseCharacter(i));
return stringBuilder.ToString();
}
private static char GetRandomUpperCaseCharacter(int seed)
{
return ((char)((short)'A' + new Random(seed).Next(26)));
}
chciałbym wiedzieć, czy istnieje lepszy sposób na jego refaktoryzację. Chciałbym zrobić to krótko i łatwiej.
Dzięki za twoje komentarze. Jednak mam wyjątek zgłoszony przez AutoFixture Ploeh.AutoFixture.ObjectCreationException: AutoFixture nie był w stanie utworzyć wystąpienia typu System.Collections.Generic.IList'1 [DDDBookingApplication.Domain.Voyage.CarrierMovement], ponieważ nie ma publicznego konstruktor. Zakładam, że powinienem powiedzieć, jak utworzyć CarrierMovement? –
Chciałbym również mieć inny zestaw danych dla wszystkich instats. Jakie są twoje myśli? –
Dzięki za wszystkie szczegóły. Test jest teraz krótki i przechodzi :) –