我正在使用 Ploeh 的 SemanticComparison 库取得了巨大的成功——除非我有一个涉及的抽象类不公开其所有构造函数参数。
这是我得到的例外 -
Ploeh.SemanticComparison.ProxyCreationException : The proxy of Foo could not be created using the same semantic heuristics as the default semantic comparison. In order to create proxies of types with non-parameterless constructor the values from the source constructor must be compatible to the parameters of the destination constructor.
----> System.InvalidOperationException : Operation is not valid due to the current state of the object.
这是我能想到的最简单的例子 -
// this fails with the aforementioned exception
_fixture.Create<Foo>().AsSource().OfLikeness<Foo>().CreateProxy();
public class Foo : Bar
{
public Foo(int age)
: base(age)
{
}
}
public abstract class Bar
{
private readonly int _age;
protected Bar(int age)
{
_age = age;
}
}
但是,如果我添加public int NotAge { get; set; }
到 abstract class Bar
,那么一切都很好。我真的认为这是一个次优的解决方案,因为我不想公开 property age
。它只是用来计算其他东西。
我怎样才能解决这个问题而不只是为了测试而暴露属性。是否有另一个库可以在没有这个问题的情况下达到相同的效果?