我写了下面的单元测试,以测试日期时间格式:为什么Assert.AreEqual()对字符串和DateTimeFormatter失败?
using System;
using Windows.Globalization.DateTimeFormatting;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
namespace MyTests
{
[TestClass]
public class DateTimeFormatterTests
{
[DataTestMethod]
[DataRow(2, 3, 2017, "en", "Thursday, March 2")]
[DataRow(2, 3, 2017, "de", "Donnerstag, 2. März")]
public void Long_date_without_year_should_match_expected(int day, int month, int year, string regionCode, string expected)
{
DateTimeFormatterformatter = new DateTimeFormatter("dayofweek month day", new[] { regionCode });
string actual = formatter.Format(new DateTime(year, month, day));
Assert.AreEqual(expected, actual);
}
}
}
我不明白为什么断言失败,出现以下错误:
{"Assert.AreEqual failed. Expected:<Thursday, March 2>. Actual:<Thursday, March 2>. "}
这是因为串具有不同编码?
转换两个串成使用编码字节数组的内容UTF8字节数组后看起来像这样:
实际:
e2 80 8e 54 68 75 72 73 64 61 79 e2 80 8e 2c 20 e2 80 8e 4d 61 72 63 68 e2 80 8e 20 e2 80 8e 32
预期: 54 68 75 72 73 64 61 79 2c 20 4d 61 72 63 68 20 32
'actual'的类型是什么? –
字符串没有编码,编码在您尝试将字符串转换为字节时发挥作用。 –
尝试将它们作为字节数组进行比较,从'Encoding.UTF8.GetBytes(actual)'和类似的期望值中查看它所抱怨的内容。 –