AutoFixture Likeness - 仅比较匹配的属性

发布于 2025-01-06 15:30:45 字数 713 浏览 0 评论 0原文

我希望能够使用 AutoFixture.SemanticComparison 比较以下两个对象的相似性:

public class Object1
{
  public int a;
}

public class Object2
{
  public int a;
  public int b;
}

现在,当我这样做时:

var o1 = new Object1 { a = 1 };
var o2 = new Object2 { a = 1, b = 2};
o1.AsSource().OfLikeness<Object2>().ShouldEqual(o2);

我得到以下异常:“以下成员不匹配:- b。”

我发现我可以像这样省略 'b' 成员:

var o1 = new Object1 { a = 1 };
var o2 = new Object2 { a = 1, b = 2};
o1.AsSource().OfLikeness<Object2>().Without(object2 => object2.b).ShouldEqual(o2);

但是,我发现这非常麻烦,因为每当我向类 Object2 添加新成员时,我都必须纠正我的单元测试(或者至少是单元测试助手) )。

有没有办法说“我想比较两个对象中存在的子集的相似性”?

I want to be able to compare the two following objects for likeness using AutoFixture.SemanticComparison:

public class Object1
{
  public int a;
}

public class Object2
{
  public int a;
  public int b;
}

Now, when I do it this way:

var o1 = new Object1 { a = 1 };
var o2 = new Object2 { a = 1, b = 2};
o1.AsSource().OfLikeness<Object2>().ShouldEqual(o2);

I get the following exception: "The following members did not match: - b."

I found out that I can omit the 'b' member like this:

var o1 = new Object1 { a = 1 };
var o2 = new Object2 { a = 1, b = 2};
o1.AsSource().OfLikeness<Object2>().Without(object2 => object2.b).ShouldEqual(o2);

However, I find that this is quite cumbersome, because whenever I add a new member to class Object2, I have to correct my unit tests (or at least unit test helpers).

Is there a way to say "I want to compare for likeness just for the subset that exists in both objects"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2025-01-13 15:30:45

听起来您想根据两个对象属性的交集来比较它们。 Likeness 类目前不支持此功能。原因是:

现在目标类型(在上面的示例中为 Object2)是完成匹配的决定性模板。这为断言提供了一个非常的声明:该类的每个公共属性或字段都必须匹配。

但是,关于匹配属性交集的声明将是一个非常强的声明 。 语句,因为交集可能为空。这可能会导致漏报

即使您正在进行 TDD 并遵循红/绿/重构循环,并且您已经看到单元测试因这种假设的相似性交集而失败,但后续重构可能会将此类断言变成假阴性,因为您删除了最后一个属性或字段这两个物体有一些共同点——你永远不会注意到。

不过,AutoFixture 是开源的,因此欢迎您建议此功能或发送拉取请求。

It sounds like you'd like to compare two objects based on the intersection of their properties. This is not currently supported by the Likeness class. The reasoning is this:

Right now the destination type (in the above example, that would be Object2) is the decisive template upon which matching is done. This provides quite a strong statement for an assertion: every public property or field of this class must be matched.

However, a statement about matching the intersection of properties would be a very weak statement, because that intersection might be empty. This could result in False Negatives.

Even if you are TDDing and following the Red/Green/Refactor cycle and you've seen a unit test failing with such a hypothetical Likeness intersection, subsequent refactorings might turn such an assertion into a False Negative, as you remove the last property or field the two objects have in common - and you'll never notice.

However, AutoFixture is open source and all, so you're welcome to suggest this feature or send a pull request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文