gpt4 book ai didi

针对字符串的 c# 单元测试日期时间

转载 作者:行者123 更新时间:2023-11-30 15:58:30 25 4
gpt4 key购买 nike

我正在对 datetime 进行单元测试,但它失败了,因为测试名称:

GetDateTime2 Result Message: Assert.AreEqual failed. Expected:<28/05/2017 20:00:00 (System.String)>. Actual:<28/05/2017 20:00:00 (System.DateTime)>.

有什么方法可以将字符串与日期时间进行比较,我是否必须更改字符串日期和字符串时间的属性?

public void GetDateTime()
{
FootballEvent football = new FootballEvent();
football.Time = "20:00:00";
football.Date = "28/05/2017";
var footballtime = football.GetDateTime();
var expected = "28/05/2017 20:00:00";
Assert.AreEqual(expected, footballtime);
}

最佳答案

正如一些人在评论中所说,为什么不直接创建一个 DateTime 对象然后比较它们呢?

示例:

var footballtime = football.GetDateTime();
var expected = "28/05/2017 20:00:00";
DateTime expectedDate = DateTime.Parse(expected);
Assert.AreEqual(expectedDate, footballtime);

关于针对字符串的 c# 单元测试日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43262013/

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