gpt4 book ai didi

c# - TimeZoneInfo 成员的单元测试的测试结果不正确

转载 作者:行者123 更新时间:2023-12-02 21:40:47 24 4
gpt4 key购买 nike

信息:使用.NET 4.0和VS 2012

嗨,

我即将对我自己的类进行单元测试,该类具有 TimeZoneInfo 类型的成员。但是,当我尝试在测试中考虑该成员时,它总是失败。

以下是一个简化的示例,用于验证 _timeZone 在实例化过程中是否已正确初始化:

public class MyClass
{
public TimeZoneInfo _timeZone;

public MyClass(string timeZoneId)
{
_timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
}
}

[TestMethod()]
public void MyClassCtorTest()
{
TimeZoneInfo expected = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
TimeZoneInfo actual = new MyClass("W. Europe Standard Time")._timeZone;

Assert.IsTrue(expected.Equals(actual)); //This test passes!

Assert.AreEqual(expected, actual); //This test fails!
}

我发现 Assert.IsTrue(...) 通过,而 Assert.AreEqual(...) 失败:“Assert.AreEqual 失败。预期:<(UTC+01:00) 阿姆斯特丹、柏林、伯尔尼、罗马、斯德哥尔摩、维也纳>。实际:<(UTC+01:00) 阿姆斯特丹、柏林、伯尔尼、罗马、斯德哥尔摩、维也纳>."

由于“Equals”在 TimeZoneInfo 类中被重写,所以我不明白这里发生了什么。你能帮我通过第二个断言吗?预先非常感谢您!

最佳答案

您通过的测试调用

bool Equals(TimeZoneInfo)

失败的测试隐式调用

bool Equals(object)

.NET 4.0 version of TimeZoneInfo , Equals(object) 尚未被覆盖;在 .NET 4.5它有。

关于c# - TimeZoneInfo 成员的单元测试的测试结果不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20488493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com