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



如果我有一个包括列表的预期对象图,则是这样的:

        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"
                },
            }
        };

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

例如,我想编写类似的东西来比较所有父对象属性和某些子对象属性:

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

但是我有一个例外:

System.ArgumentException : Expression <o.Experimenters.Select(e => e.Role)> cannot be used to select a member.

我不介意在这种情况下,子项目的顺序是什么,但我想断言我关心的属性对匹配期望。

您可以通过检查运行时类型和当前对象的选定成员路径来做到这一点:

.Including(subjectInfo => subjectInfo.RuntimeType == typeof(Experimenter) &&
                          subjectInfo.SelectedMemberPath.EndsWith("Role"))

这可以提取到进一步重复使用的方法中:

private Expression<Func<ISubjectInfo, bool>> BuildMemberExpression<TSource, TProperty>(Expression<Func<TSource,TProperty>> propertySelector)
    {
        var memberExpression = propertySelector.Body as MemberExpression;
        if (memberExpression == null)
        {
            throw new NotSupportedException();
        }
        return subjectInfo => subjectInfo.RuntimeType == typeof(TSource) && 
                              subjectInfo.SelectedMemberPath.EndsWith(memberExpression.Member.Name);
    }

现在以这种方式使用:

.Including(BuildMemberExpression((Experimenter e) => e.Name))
.Including(BuildMemberExpression((Experimenter e) => e.Role))

相关内容

  • 没有找到相关文章

最新更新