단위 테스트가 많은 데이터 설정으로 부풀어 오르면서 AutoFixture http://autofixture.codeplex.com/을 사용하기 시작했습니다. 내 단위 테스트를 작성하는 것보다 데이터를 설정하는 데 더 많은 시간을 보냈습니다. 여기 내 초기 단위 테스트 (DDD 블루 책에서화물 응용 프로그램 샘플에서 가져온 예) 다음자동 수정 리팩토링
[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();
}
처럼 보이는 방법의 예 내가 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)));
}
싶습니다에 그것을 리팩토링을 시도 방법입니다 리팩터링하는 것이 더 좋은 방법인지 알아보십시오. 그것보다 짧고 쉽게하고 싶습니다.
귀하의 의견에 감사드립니다. 그러나 약간의 예외가 AutoFixture에 의해 throw되었습니다. Ploeh.AutoFixture.ObjectCreationException : AutoFixture는 Public이 없으므로 System.Collections.Generic.IList'1 [DDDBookingApplication.Domain.Voyage.CarrierMovement] 형식의 인스턴스를 만들 수 없습니다. 건설자. CarrierMovement를 만드는 방법을 말해야한다고 가정합니다. –
나는 모든 instares에 대해 다른 데이터 세트를 갖고 싶습니다. 너 생각이 뭐니? –
모든 세부 정보 주셔서 감사합니다. 테스트가 짧고 통과합니다 :) –