MVC SelectListItem 没有实现等于吗?

发布于 2025-01-02 04:37:14 字数 435 浏览 1 评论 0原文

忙于为控制器编写单元测试,该控制器生成一个视图模型,其中包含 IEnumerable < 等选项列表。选择列表项>。我尝试检查预期列表是否包含视图模型中的所有列表,反之亦然。令我惊讶的是,这总是错误的。所以我创建了以下测试:

[TestMethod]
public void CanEqual()
{
  var x = new SelectListItem {Selected = false, Text = "A", Value = "A"};
  var y = new SelectListItem { Selected = false, Text = "A", Value = "A" };
  Assert.AreEqual(x, y); 
}

断言总是失败,但两者相等。 SelectListItem 真的没有实现 Equals 还是我只是在这里遗漏了一些东西?

Busy writing a unit test for a controller that produces a view-model that includes a list of options as IEnumerable < SelectListItem >. I tried checking that the expected list contains all the ones in the view-model and vice-versa. To my surprise this is always false. So I created the following test:

[TestMethod]
public void CanEqual()
{
  var x = new SelectListItem {Selected = false, Text = "A", Value = "A"};
  var y = new SelectListItem { Selected = false, Text = "A", Value = "A" };
  Assert.AreEqual(x, y); 
}

The assertion always fails but the two are equal. Does SelectListItem really not implement Equals or am I just missing something here?

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

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

发布评论

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

评论(2

深爱成瘾 2025-01-09 04:37:14

添加到 Shark 的答案...至于该怎么办,除了在派生类上实现 IEquatable之外(如果这样做,您真的应该重写非泛型 Equals() 也是如此 - 如果你这样做,你真的应该重写 GetHashCode())...无论如何...除了这样做之外,您可以:

  1. 在您的测试项目中创建一个辅助方法进行值比较(可以使用适用于大多数简单类的反射编写通用目的),或者
  2. 为您需要的每种类型创建一个实现 IEqualityComparer 的辅助类比较。

两者都不允许您使用 Assert.AreEqual(),但总的来说,我不赞成仅仅为了允许测试而向对象添加代码 - 更喜欢将其保留在测试项目中。另外,通过这些方法,您将不再“需要”实现 GetHashCode() 等。

Adding to Shark's answer... As for what to do about it, other than implementing IEquatable<T> on a derived class (and if you do that, you really should override the non-generic Equals() too - and if you do that, you really should override GetHashCode())... Anyway... other than doing that, you could:

  1. Create a helper method in your test project to do the value comparison (it's possible to write a general purpose one using Reflection that would work for most simple classes), or
  2. Make a helper class implementing IEqualityComparer<T> for each of the types you need to compare.

Neither will allow you to use Assert.AreEqual(), but in general, I'm not in favor of adding code to your objects just to allow testing - prefer keeping it in the test project. Plus, with these approaches, you won't "need" to implement GetHashCode() etc.

浅浅 2025-01-09 04:37:14

这是因为默认情况下,它正在测试相同的参考。在这种情况下,您有该对象的两个实例,因此它们不“相等”。

如果您想更改该行为,只需实现 IEquatable 接口,并且可以重新定义 Equals() 将返回的内容。例如:

public bool Equals(YourClass other)
{
    return (this.Value == other.Value);
}

有关 Object.Equals() 的良好参考,请参阅 此 MSDN 参考。引用类型的相等性基于它们的引用。即,如果它们不引用同一对象,则 Equals() 将返回 false

That's because by default, it is testing for the same reference. In this case, you have two instances of the object, therefore they aren't "equal".

If you want to alter that behavior, you just need to implement the IEquatable<T> interface, and you can redefine what Equals() will return. For instance:

public bool Equals(YourClass other)
{
    return (this.Value == other.Value);
}

For a good reference on Object.Equals(), please see this MSDN reference. Equality for reference types is based on their reference. I.e. if they don't reference the same object then Equals() will return false.

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