我为以下要在测试中使用的对象构建了自己的比较。它按照当前的方式工作,并在比较字段之一不匹配时传递错误值。有没有一种方法可以为我提供有关比较失败的字段的更详细信息?
[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);
}
}
测试:
[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 失败输出如下所示:
Test Name: GoodDataTests Test FullName: ThunderBallApiTests.StatisticsTests.GoodDataTests Test Source: \sky.dom\mlfile1\users\DanS\My Documents\Visual Studio 2012\Projects\ThunderBallApiTests\ThunderBallApiTests\StatisticsTests.cs : line 20 Test Outcome: Failed Test Duration: 0:00:20.203
Result1 Name: GoodDataTests(SubscriptionId: 167769, ProfileId: 1571394, ClientID: 1234, statusCode: "Active", prospectCount: 54, messageCount: 17, newListingCount: 0, reminderCount: 33, myListingCount: 0, officListingCount: 2) Result1 Outcome: Failed Result1 Duration: 0:00:01.471 Result1 Message:
Assert.Equal() Failure Expected: ThunderBallApiTests.DataContainers.Stats Actual: ThunderBallApiTests.DataContainers.Stats Result1 StackTrace: at ThunderBallApiTests.StatisticsTests.StatisticsGoodDataTests(Int32 SubscriptionId, Int32 ProfileId, Int32 Id, String statusCode, Int32 prospectCount, Int32 messageCount, Int32 newListingCount, Int32 reminderCount, Int32 myListingCount, Int32 officListingCount) in \sky.dom\mlfile1\users\DanS\My Documents\Visual Studio 2012\Projects\ThunderBallApiTests\ThunderBallApiTests\StatisticsTests.cs:line 36
最佳答案
请注意,对数据合约进行相等操作通常不是一个好闻的事情。 xUnit Test Patterns Book在这个领域提供了很好的一般建议和模式。这本书涵盖了特定于测试的平等和平等污染的概念。它还具有 Custom Assertion 的概念。 ,它可以满足您的需求(假设生产代码中不直接需要等式)。
其他有用的技巧是:
- 覆盖
ToString
和Assert.Equal
将免费提供更好的诊断(您可以使用 CR 或 R# tem[plate 来执行此操作) - 映射到
Tuple
或匿名类并免费获得所述ToString
impl。
就自动化此类操作的工具而言,您可以使用 AutoFixture's Likeness
(对于这种情况来说,这有点过分了,但对于 map 绘制者来说可能非常好,并且是值得注意的潜在金锤;)。另外,如果您使用 F# ( you really should ) 编写测试,则可以 lean on unquote .
关于c# - 如何从自定义比较中获取有关失败的更详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275837/