为什么 Assert.AreEqual(T obj1, Tobj2) 对于相同的对象会失败
我有一个包含多个结构的类:
public class NavigationMenu
{
public struct NavigationMenuItem
{
public bool Enabled { get; set; }
public int NumberOfPendingOperations { get; set; }
}
public NavigationMenuItem SubmitExpenses { get; set; }
public NavigationMenuItem ManageExpenses { get; set; }
public NavigationMenuItem SubmitBudgets { get; set; }
public NavigationMenuItem ManageBudgets { get; set; }
public NavigationMenuItem AuthorizeExpenses { get; set; }
public NavigationMenuItem ApproveExpenses { get; set; }
public NavigationMenuItem PayExpenses { get; set; }
public NavigationMenuItem BillExpenses { get; set; }
public NavigationMenuItem ManageReturnedExpenses { get; set; }
public NavigationMenuItem ManageIncompleteExpenses { get; set; }
public NavigationMenuItem ManageOrders { get; set; }
public NavigationMenuItem ApproveBudgets { get; set; }
public NavigationMenuItem AdministrateSystem { get; set; }
}
在单元测试中,我进行函数调用并比较结果:
NavigationMenu expected = new NavigationMenu();
expected.SubmitExpenses = new NavigationMenu.NavigationMenuItem { Enabled = true };
expected.ManageExpenses = new NavigationMenu.NavigationMenuItem { Enabled = true };
NavigationMenu actual = HomeControllerHelper.GetNavigationMenuByUserRole(userRole);
Assert.AreEqual(expected, actual);
但是 Assert.AreEqual 总是抛出 AssertFailedException。这些对象是相同的,我已经使用调试器验证了这一点。请分享任何想法。谢谢。
I have a class which contains several structs:
public class NavigationMenu
{
public struct NavigationMenuItem
{
public bool Enabled { get; set; }
public int NumberOfPendingOperations { get; set; }
}
public NavigationMenuItem SubmitExpenses { get; set; }
public NavigationMenuItem ManageExpenses { get; set; }
public NavigationMenuItem SubmitBudgets { get; set; }
public NavigationMenuItem ManageBudgets { get; set; }
public NavigationMenuItem AuthorizeExpenses { get; set; }
public NavigationMenuItem ApproveExpenses { get; set; }
public NavigationMenuItem PayExpenses { get; set; }
public NavigationMenuItem BillExpenses { get; set; }
public NavigationMenuItem ManageReturnedExpenses { get; set; }
public NavigationMenuItem ManageIncompleteExpenses { get; set; }
public NavigationMenuItem ManageOrders { get; set; }
public NavigationMenuItem ApproveBudgets { get; set; }
public NavigationMenuItem AdministrateSystem { get; set; }
}
In a unit test I make a function call and compare the results:
NavigationMenu expected = new NavigationMenu();
expected.SubmitExpenses = new NavigationMenu.NavigationMenuItem { Enabled = true };
expected.ManageExpenses = new NavigationMenu.NavigationMenuItem { Enabled = true };
NavigationMenu actual = HomeControllerHelper.GetNavigationMenuByUserRole(userRole);
Assert.AreEqual(expected, actual);
But the Assert.AreEqual always throws AssertFailedException. The objects are identical, I've verified this using debugger. Please share any ideas. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用 Assert.AreEqual(expected, Expected) 不应失败。如果您在问题中犯了一个错误,并且您的意思是 Assert.AreEqual(expected,actual) 并且您的 HomeControllerHelper.GetNavigationMenuByUserRole 返回一个新的 NavigationMenu 实例,那么对 Assert.AreEqual 的调用总是会失败,因为您的类型 NavigationMenu 是一个类,因此是一个引用类型,即使您将实例的属性设置为相同的值也是如此。
如果两个变量指向相同的引用(也称为 ReferenceEqual),则 Assert.AreEqual 执行相等性检查;如果两个引用包含相同的(属性)值,则不执行相等性检查。
如果类的两个实例相等,您可以重写 NavigationMenu 类的 Equals 方法来提供自定义实现。
The call Assert.AreEqual(expected, expected) should not fail. If you made a mistake in your question and you meant Assert.AreEqual(expected, actual) and your HomeControllerHelper.GetNavigationMenuByUserRole returns a new instance of NavigationMenu, then will the call to Assert.AreEqual always fail, cause your type NavigationMenu is a class and therefore a reference type, even if you set the properties of the instances to the same values.
Assert.AreEqual performs an equality check if the two variables point to the same reference (aka. ReferenceEqual) and not if the two references contain the same (property) values.
You could override the Equals method of your NavigationMenu class to provide a custom implementation if two instances of your class are equal.
因为您(可能)正在比较一个对象的两个不同实例,但具有相同的参数。为了使对象“相等”,您需要重写对象上的 Equals 方法并在那里实现比较。
Because you are (probably) comparing two different instances of an object, but with the same parameters. In order for the objects to be "equal", you need to override the Equals method on the object and implement a comparison there.
假设它应该是
Assert.AreEqual(expected,actual);
,就像上面注释中所述:您必须定义如何比较
NavigationMenuItem
对象。 Atm 是唯一检查是否是同一个实例的情况,显然他们不是那么断言必须失败。Assuming that it should be
Assert.AreEqual(expected, actual);
, like it was stated in comments above:You have to define how to compare
NavigationMenuItem
objects. Atm its only cheking if its the same instance and obviosly they aren't so asserty have to fail.