【发布时间】:2021-11-04 04:17:48
【问题描述】:
我刚刚将一个 dotnet core 3.1 项目迁移到 dotnet 5.0,从那以后我的单元测试遇到了问题。我的单元测试之一是测试反序列化 AccountDto 对象时返回的错误(转换器测试)。
在我的 dotnet 5.0 迁移之前,我在单元测试捕获中收到以下错误消息:“不支持记录类型 id ''”。自迁移以来,捕获的异常已更改:“不支持记录类型 id ''。不受支持的成员类型位于类型 'System.String'。路径:$.RecordTypeId | LineNumber: 0 | BytePositionInLine: 64."
通过分析异常的内容,我注意到我在 RecordTypeConverter.Read() 中抛出的异常确实存在,但在内部异常中。
单元测试:
[Theory]
[InlineData("", null, "The record type id '' is not supported.")]
protected void MapAccountRecordTypeTest(string recordTypeId, string recordTypeExpected, string errorExpected)
{
var account = new AccountDto();
var error = string.Empty;
var accountDto = new AccountDto
{
RecordType = recordTypeId
};
var myjson = JsonSerializer.Serialize(accountDto);
try
{
account = JsonSerializer.Deserialize<AccountDto>(myjson);
}
catch (Exception ex)
{
error = ex.Message;
}
Assert.Equal(errorExpected, error);
}
单元测试调用的代码并抛出我想捕获的异常:
public class RecordTypeConverter : JsonConverter<string>
{
private readonly Dictionary<string, string> _accountStatus = new()
{
{ Constant.ENTERPRISE_RECORD_TYPE_ID, Constant.ENTERPRISE_RECORD_TYPE },
{ Constant.INDIVIDUAL_RECORD_TYPE_ID, Constant.INDIVIDUAL_RECORD_TYPE }
};
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string value = reader.GetString();
if (value != null)
{
string key = value.ToUpper();
if (!_accountStatus.ContainsKey(key))
{
throw new NotSupportedException($"The record type id '{key}' is not supported.");
}
return _accountStatus[key];
}
return null;
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
=> writer.WriteStringValue(value);
}
捕捉到异常:
à System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, NotSupportedException ex)
à System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
à System.Text.Json.JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
à System.Text.Json.JsonSerializer.ReadCore[TValue](Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options)
à System.Text.Json.JsonSerializer.Deserialize[TValue](String json, Type returnType, JsonSerializerOptions options)
à System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
à tests.AccountTests.MapAccountRecordTypeTest(String recordTypeId, String recordTypeExpected, String errorExpected) dans D:\\XXXXX\\AccountTests.cs :ligne 72
异常存在于上述异常的内部异常中。我应该检索的正是这个异常:
à SlxConnector.Helpers.Converters.RecordTypeConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) dans d:\\XXXXXXX\\src\\Helpers\\Converters\\RecordTypeConverter.cs :ligne 27
à System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
à System.Text.Json.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
à System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
à System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
à System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
我知道如何调整我的单元测试以使其工作,但我想了解为什么当我将我的项目迁移到 .NET 5 时,行为发生了变化。显然,.NET 5 JsonSerializer 从我的异常中引发了一个新异常???
【问题讨论】:
-
.NET 5 和 C# 5 是不同的东西,特别是 .NET 5 是与 C# 9 一起发布的。
-
要明确:你的意思是 - 它仍然抛出预期的异常,但消息现在更具体?听起来您只需更新单元测试即可处理新消息——也许使用
StartsWith。抱歉,如果我误解了您的意思 - 如果是,请澄清。 -
感谢我最初发布的修复和答案。我知道如何调整我的单元测试以使其工作,但我想了解为什么当我将项目迁移到 .NET 5 时,行为发生了变化。显然,.NET 5 JsonSerializer 从我的异常中引发了一个新异常。
标签: c# exception .net-core try-catch .net-5