两个集合类之间的流畅断言

发布于 2025-01-10 15:36:30 字数 1295 浏览 3 评论 0原文

我有一个带有集合类的类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝梦月影 2025-01-17 15:36:30

您需要基于旧实例创建类的新实例:

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();

You need to create new instances of the classes based on the old instances:

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();
远昼 2025-01-17 15:36:30
result.Should().BeEquivalentTo({SearchResultClone}) 

如何编写这个流畅的断言?

如果您的期望(传递给 BeEquivalentTo 的对象)是 SearchResult 类型,那么 FA 将尝试将 ISBN 的空值与sut 上具有相同的属性。您可以通过执行以下操作来解决该问题:

sut.Should().BeEquivalentTo(new 
{
  Id = "some value",
  Total = 123,
  Books = new[] 
  {
    new 
    {
      BookId = 123,
      BookName = "some book"
    }
  }
});
result.Should().BeEquivalentTo ({the SearchResultClone }) 

How to write this fluent assertion ?

If your expectation (the object you pass into BeEquivalentTo) is of the type SearchResult, then FA will try to compare the empty values of ISBN to the same property on the sut. You can solve that by doing something like:

sut.Should().BeEquivalentTo(new 
{
  Id = "some value",
  Total = 123,
  Books = new[] 
  {
    new 
    {
      BookId = 123,
      BookName = "some book"
    }
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文