2014-04-24 2 views
2

필자는이 테스트를 위해 다음과 같은 객체에 대한 필자의 비교를 구축했다. 그것은 현재 서 있고 비교 된 필드 중 하나가 일치하지 않을 때 잘못된 값을 전달합니다. 비교를 실패한 필드에 대한 자세한 정보를 제공하는 방법이 있습니까?맞춤 비교의 실패에 대한 자세한 정보는 어떻게 얻을 수 있습니까?

[DataContract] 
public class Stats : IEquatable<Stats> 
{ 
    [DataMember] 
    public string StatusCode { get; set; } 
    [DataMember] 
    public int ProspectCount { get; set; } 
    [DataMember] 
    public int MessageCount { get; set; } 
    [DataMember] 
    public int NewListingCount { get; set; } 
    [DataMember] 
    public int ReminderCount { get; set; } 
    [DataMember] 
    public int MyListingCount { get; set; } 
    [DataMember] 
    public int OfficeListingCount { get; set; } 

    public bool Equals(Stats other) 
    { 
     if (Object.ReferenceEquals(other, null)) return false; 

     if (Object.ReferenceEquals(this, other)) return true; 

     return StatusCode.Equals(other.StatusCode) && 
       ProspectCount.Equals(other.ProspectCount) && 
       MessageCount.Equals(other.MessageCount) && 
       NewListingCount.Equals(other.NewListingCount) && 
       ReminderCount.Equals(other.ReminderCount) && 
       MyListingCount.Equals(other.MyListingCount) && 
       OfficeListingCount.Equals(other.OfficeListingCount); 
    } 

} 

는 테스트 :

시험 이름 : GoodDataTests 테스트하면 FullName : ThunderBallApiTests.StatisticsTests.GoodDataTests 테스트 소스

[Theory] 
[ExcelData("Stats.xls", "Select * from TestData")] 
public void GoodDataTests(int SubscriptionId, int ProfileId, int ClientID, string statusCode, int prospectCount, 
    int messageCount, int newListingCount, int reminderCount, int myListingCount, int officListingCount) 
{ 
    DataContainers.Stats expectedStats = new DataContainers.Stats{ 
     StatusCode = statusCode, 
     ProspectCount = prospectCount, 
     MessageCount = messageCount, 
     NewListingCount = newListingCount, 
     ReminderCount = reminderCount, 
     MyListingCount = myListingCount, 
     OfficeListingCount = officListingCount 
    };  

    string url = Utils.CreateStatisticsUrlRequest(SubscriptionId,ProfileId,ClientID); 
    string response = Utils.GetResponseBody(url); 

    DataContainers.Stats results = JsonConvert.DeserializeObject<DataContainers.Stats>(response); 

    Assert.Equal(expectedStats, results); 
} 

xUnit의에서 나의 현재 오류 출력은 다음과 같은 : \ sky.dom \ mlfile1 \ users \ DanS \ 내 문서 \ Visual Studio 2012 \ Projects \ ThunderBallApiTests \ ThunderBallApiTests \ StatisticsTests.cs : 줄 20,테스트 결과 : 실패 시험 시간 : 0 : 00 : 20.203

결과 1 이름 : GoodDataTests (SubscriptionId : 167769, profileID가 : 1,571,394, 된 ClientID : 1234에 statusCode : "활성", prospectCount : 54, messageCount : 17 newListingCount : 0, reminderCount : 33, myListingCount : 0, officListingCount : 2) 결과 1의 결과 : 실패 결과 1 시간 : 0 : 00 : 01.471 결과 1 메시지 : 예상
Assert.Equal() 실패 : ThunderBallApiTests.DataContainers.Stats 실제 : ThunderBallApiTests.DataContainers.Stats 결과 1 StackTrace : ThunderBallApiTests.StatisticsTests.StatisticsGoodDataTests (Int32 SubscriptionId, Int3 2 SkyDevice \ ThunderBallApiTests \ IntelliSupportTests \ SkyDom \ MyFile \ 사용자 \ DanS \ My Documents \ Visual Studio 2012 \ Projects \ ThunderBallApiTests \ ThunderBallApiTests \ StatisticsTests.cs : line 36

답변

2

데이터 계약에 동등한 연산을 두는 것이 일반적으로 좋은 냄새는 아닙니다. xUnit Test Patterns Book은이 공간에서 좋은 조언과 패턴을 제공합니다. 이 책은 시험 관련 평등과 평등 오염이라는 개념을 다룹니다. 또한 Custom Assertion이라는 개념을 사용하여 필요를 충족시킬 수 있습니다 (생산 코드에서 평등이 직접 필요하지 않다고 가정).

다른 유용한 트릭은 다음과 같습니다

  • 재정 ToStringAssert.EqualTuple 나에
  • 지도 (당신은 그렇게 할 수있는 CR 또는 R #의 편 [플레이트를 사용할 수 있습니다) 무료로 더 나은 진단을 줄 것이다 익명의 클래스가 무료로 ToString impl을 얻는다. (그것은이 시나리오에 대한 잔인한하지만 매퍼 우수한 할 수 있고 인식하고 가치가 잠재적 인 황금 망치입니다) 도구의 관점에서

당신은 AutoFixture's Likeness를 사용할 수있는이 같은 물건을 자동화합니다.또한 F # (you really should)으로 테스트를 작성하는 경우 lean on unquote 수 있습니다.

+0

좋은 물건. 감사. –

0

나는 이것이 실제로 두 가지 개념 - 비교 및 ​​테스트를 혼란스럽게한다고 생각합니다. 평등에 대한 "테스트"는 참/거짓 값을 갖지만 둘 다 "실패"입니다. 더 넓은 시나리오는 두 객체가 테스트 케이스를 만족시키기 위해 동등해야한다는 것이고, 두 객체가 동일하지 않다는 것을 알게되면 각각의 속성이 동등한 지 테스트합니다. 그러면 더 상세한 테스트 결과를 얻을 수 있습니다.

관련 문제