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

标签 c# testing nunit fluent-assertions

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

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

但我得到一个异常(exception):

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

关于c# - 如何使用流畅的断言比较对象图中的嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38012230/

相关文章:

c# - 将具有空值的参数添加到 sqldatasource 控件的更好方法

c# - 关于 StreamReader

java - 如何将测试覆盖率添加到私有(private)构造函数?

c#-4.0 - Nunit 2.6.2 Suite 运行两次而不是一次

c# - 你如何用 Rhino Mocks stub IQueryable<T>.Where(Func<T, bool>)?

c# - 如何更改字符串中单词的顺序?

c# - 如何一次选择2个项目?

testing - 如何在 xpages 中进行负载测试

c# - 如何在 WinRT 应用程序中使用 nUnit 测试?

c# - 使用 Typemock 模拟静态类