两个 collection class 之间的流利断言
Fluent Assertions between two collection class
我有一个 class 里面 collection class
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; }
}
我有一个问题,如果我创建另一个 object,具有相同的 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 ..等值
如何在 LINQ 中实现?
我的第二个问题是,如果我想像上面那样做出流畅的断言object
var result = await sut.search(query);
result.Should().BeEquivalentTo ({the SearchResultClone })
这个流利的断言怎么写?
您需要根据旧实例创建 类 的新实例:
var ans = result.Select(sr => new SearchResult {
Id = sr.Id,
Total = sr.Total,
Books = sr.Books.Select(b => new Book { BookId = b.BookId, BookName = b.BookName }).ToList()
}).ToList();
result.Should().BeEquivalentTo ({the SearchResultClone })
How to write this fluent assertion ?
如果你的期望(你传递给BeEquivalentTo
的对象)是SearchResult
类型,那么FA会尝试将ISBN
的空值与相同的属性 在 sut
上。您可以通过执行以下操作来解决该问题:
sut.Should().BeEquivalentTo(new
{
Id = "some value",
Total = 123,
Books = new[]
{
new
{
BookId = 123,
BookName = "some book"
}
}
});
我有一个 class 里面 collection class
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; }
}
我有一个问题,如果我创建另一个 object,具有相同的 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 ..等值 如何在 LINQ 中实现?
我的第二个问题是,如果我想像上面那样做出流畅的断言object
var result = await sut.search(query);
result.Should().BeEquivalentTo ({the SearchResultClone })
这个流利的断言怎么写?
您需要根据旧实例创建 类 的新实例:
var ans = result.Select(sr => new SearchResult {
Id = sr.Id,
Total = sr.Total,
Books = sr.Books.Select(b => new Book { BookId = b.BookId, BookName = b.BookName }).ToList()
}).ToList();
result.Should().BeEquivalentTo ({the SearchResultClone })
How to write this fluent assertion ?
如果你的期望(你传递给BeEquivalentTo
的对象)是SearchResult
类型,那么FA会尝试将ISBN
的空值与相同的属性 在 sut
上。您可以通过执行以下操作来解决该问题:
sut.Should().BeEquivalentTo(new
{
Id = "some value",
Total = 123,
Books = new[]
{
new
{
BookId = 123,
BookName = "some book"
}
}
});