Tôi bắt đầu sử dụng AutoFixture http://autofixture.codeplex.com/ vì các thử nghiệm đơn vị của tôi bị cồng kềnh với rất nhiều thiết lập dữ liệu. Tôi đã dành nhiều thời gian hơn để thiết lập dữ liệu hơn là viết bài kiểm tra đơn vị của mình. Dưới đây là một ví dụ về cách kiểm tra đơn vị ban đầu của tôi trông như thế (ví dụ lấy từ mẫu ứng dụng hàng hóa từ DDD cuốn sách màu xanh)Tái cấu trúc 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();
}
Dưới đây là làm thế nào tôi đã cố gắng để cấu trúc lại nó với 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)));
}
Tôi muốn biết nếu có cách tốt hơn để cấu trúc lại nó. Muốn làm điều đó ngắn hơn và dễ hơn thế.
Cảm ơn ý kiến của bạn. Tuy nhiên tôi có một ngoại lệ nhỏ được ném bởi AutoFixture Ploeh.AutoFixture.ObjectCreationException: AutoFixture không thể tạo một thể hiện kiểu System.Collections.Generic.IList'1 [DDDBookingApplication.Domain.Voyage.CarrierMovement], vì nó không có công khai constructor. Tôi cho rằng tôi nên nói cách tạo CarrierMovement? –
Tôi cũng muốn có bộ dữ liệu khác nhau cho tất cả các trường hợp. Suy nghĩ của bạn là gì –
Cảm ơn tất cả các chi tiết. Bài kiểm tra ngắn và trôi qua :) –