两个集合类之间的流畅断言
我有一个带有集合类的类,
public class SearchResult {
public int Id { get; set; }
public int Total { get; set; }
public IEnumerable<Book> Books { get; set; }
}
public class Book {
public int BookId { get; set; }
public string BookName { get; set; }
public string Publisher { get; set; }
public string ISBNCode { get; set; }
public IList<catagory> Catagories { get; set; }
}
我有一个问题,如果我创建另一个具有相同结构的 SearchResult 对象,并且我想将 SearchResult 复制到 SearchResultClone,其中 Books 中仅复制 BookId 和 BookName 保留为空。 就像下面的
{
"Id": 0,
"Total": 3,
"Books": [
{
"BookId": 1,
"BookName": "Book A",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
},
{
"BookId": 2,
"BookName": "Book B",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
},
{
"BookId": 3,
"BookName": "Book C",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
}
]
}
事件一样,原始结果的值为 Publisher、ISBNCode ..etc 如何在 LINQ 中做到这一点?
我的第二个问题是,如果我想像上面的对象一样进行流畅的断言,
var result = await sut.search(query);
result.Should().BeEquivalentTo ({the SearchResultClone })
如何编写这个流畅的断言?
I have a class with collection class inside
public class SearchResult {
public int Id { get; set; }
public int Total { get; set; }
public IEnumerable<Book> Books { get; set; }
}
public class Book {
public int BookId { get; set; }
public string BookName { get; set; }
public string Publisher { get; set; }
public string ISBNCode { get; set; }
public IList<catagory> Catagories { get; set; }
}
I have a question , if I create the other object , with same structure of SearchResult and I want to copy SearchResult to SearchResultClone, which inside Books only copy BookId and BookName remain is empty.
Just like below
{
"Id": 0,
"Total": 3,
"Books": [
{
"BookId": 1,
"BookName": "Book A",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
},
{
"BookId": 2,
"BookName": "Book B",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
},
{
"BookId": 3,
"BookName": "Book C",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
}
]
}
Event the original result have value of Publisher, ISBNCode ..etc
How to do it in LINQ ?
My second question is , if I want to make a fluent assertions as above object
var result = await sut.search(query);
result.Should().BeEquivalentTo ({the SearchResultClone })
How to write this fluent assertion ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要基于旧实例创建类的新实例:
You need to create new instances of the classes based on the old instances:
如果您的期望(传递给
BeEquivalentTo
的对象)是SearchResult
类型,那么 FA 将尝试将ISBN
的空值与sut
上具有相同的属性。您可以通过执行以下操作来解决该问题:If your expectation (the object you pass into
BeEquivalentTo
) is of the typeSearchResult
, then FA will try to compare the empty values ofISBN
to the same property on thesut
. You can solve that by doing something like: