小编Ed *_*les的帖子

如何使用流畅的断言比较对象图中的嵌套列表

如果我有一个包含列表的预期对象图,如下所示:

        var expectedExperiment = new Experiment
        {
            Number= "12345",
            AllocatedInstrument = "Instrument 1",
            Experimenters = new List<Experimenter>
            {
                new Experimenter
                {
                    Name = "Sue"
                    Role = "Scientist",
                    Id = 1,
                    Initials = "S"
                },
                new Experimenter()
                {
                    Name = "Mark",
                    Role = "Technician",
                    Id = 2,
                    Initials = "M"
                },
            }
        };
Run Code Online (Sandbox Code Playgroud)

当我只想在子对象列表中包含某些属性时,如何将其与实际对象进行比较。

例如,我想编写这样的代码来比较所有父对象属性和一些子对象属性:

        actualExperiment.ShouldBeEquivalentTo(expectedExperiment, options => options
            .Including(o => o.Number)
            .Including(o => o.AllocatedInstrument)
            .Including(o => o.Experimenters.Select(e => e.Role))
            .Including(o => o.Experimenters.Select(e => e.Name)));
Run Code Online (Sandbox Code Playgroud)

但我得到一个例外:

System.ArgumentException : Expression <o.Experimenters.Select(e => …
Run Code Online (Sandbox Code Playgroud)

c# testing nunit fluent-assertions

5
推荐指数
1
解决办法
1482
查看次数

标签 统计

c# ×1

fluent-assertions ×1

nunit ×1

testing ×1