为什么FluentAssertions应该等同于验证内部?

时间:2017-01-30 10:05:01

标签: c# tdd fluent-assertions

最近,我回答SO question描述如何以避免FluentAssertions中的内部对象状态验证。现在我遇到了同样的问题,并想知道为什么 FluentAssertions验证内部属性OOTB?

public class Class1
{
    [Fact]
    public void CompareCultureInternalFields()
    {
        var foo1 = new Foo();
        var foo2 = new Foo();

        foo1.ShouldBeEquivalentTo(foo2); // fails
    }

    public object Culture { get; set; }
}

public class Foo
{
    public Foo()
    {
        InternalProp = Guid.NewGuid();
    }

    internal Guid InternalProp { get; }
}

异常详情:

Xunit.Sdk.XunitException: Expected member InternalProp to be {61625b04-c4e6-4e08-a45a-5ff8bb7d53e7}, but found {df589d73-e382-4104-8157-a41da2ca17f5}.

With configuration:
- Use declared types and members
- Compare enums by value
- Match member by name (or throw)
- Be strict about the order of items in byte arrays

对于处理公共API的消费者,foo1foo2对象不应该等同于吗?

1 个答案:

答案 0 :(得分:1)

我试图将它追溯到回购的起源,但显然它一直都是这样的。在某种程度上,internal属性在概念上是 public 。如果您确实希望将它们设计为不属于您的公共API,请将它们设为私有。您可能遇到此问题的另一个原因是您测试范围可能有点偏小。你为什么要公开一些属性而其他属于内部属性?同样,这只是我的推定,你可能有充分的理由这样做。您可以随时排除internal属性。

相关问题