這是我的單元測驗,它失敗并顯示一條訊息。請幫我解決這個問題。
更新問題:
看起來我正在嘗試對該JsonSerializer.Deserialize方法進行單元測驗。那么這是一個合法的單元測驗嗎?
資訊:
Expected got to be equal to {
JsonFromTo.CMSContacts.ContactsFilter
{
cms = "asees",
groups = {"jio", "jiso"}
},
JsonFromTo.CMSContacts.ContactsFilter
{
cms = "bsees",
groups = {"jto"}
}
}
, but {
JsonFromTo.CMSContacts.ContactsFilter
{
cms = "asees",
groups = {"jio", "jiso"}
},
JsonFromTo.CMSContacts.ContactsFilter
{
cms = "bsees",
groups = {"jto"}
}
}
differs at index 0.
被測系統:
public IEnumerable<ContactsFilter> GetFilters(string json)
{
return JsonSerializer.Deserialize<List<ContactsFilter>>(json);
}
public class ContactsFilter
{
public string cms { get; set; }
public List<string> groups { get; set; }
}
單元測驗:
public class CmsContactsTest
{
public const string Filters = @"[{""cms"": ""asees"",""groups"": [""jio"",""jiso""]},{""cms"": ""bsees"",""groups"": [""jto""]}]";
[Fact]
public void Should_Return_List()
{
//arrange
var want = new List<ContactsFilter>()
{
new ContactsFilter()
{
cms = "asees",
groups = new List<string>{ "jio", "jiso" }
},
new ContactsFilter()
{
cms = "bsees",
groups = new List<string>{ "jto"}
}
};
var got = new CmsContacts().GetFilters(Filters);
got.Should().Equal(want);
}
}
uj5u.com熱心網友回復:
你應該使用BeEquivalentTo
got.Should().BeEquivalentTo(want);
參考BeEquivalentTo源代碼:
// Summary:
// Asserts that a collection of objects is equivalent to another collection of objects.
// ...
// Remarks:
// Objects within the collections are equivalent when both object graphs have equally
// named properties with the same value, irrespective of the type of those objects.
// Two properties are also equal if one type can be converted to another and the
// result is equal. The type of a collection property is ignored as long as the
// collection implements System.Collections.Generic.IEnumerable`1 and all items
// in the collection are structurally equal. Notice that actual behavior is determined
// by the global defaults managed by FluentAssertions.AssertionOptions.
With Equal, 元素使用它們的比較System.Object.Equals(System.Object)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/498021.html
標籤:C# 林克 asp.net 核心 链接到实体 xunit
上一篇:LINQ按年份分組形成樹形視圖
