【发布时间】:2021-04-01 12:05:05
【问题描述】:
我目前处于项目的集成测试阶段,在尝试测试端点以查看我的数据库是否返回某些任务时遇到了错误。 我的域对象如下所示:
public class Task
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal? InitialPrice { get; set; }
}
我的端点基本测试代码
[Fact]
public async Task Should_return_All_Tasks_with_ok_statusCode()
{
client.SetFakeBearerToken((object) token);
var response = await client.GetAsync("/api/v1/tasks");
response.StatusCode.Should().Be(HttpStatusCode.OK);
var json = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<List<Task>>(json);
data.Count.Should().BeGreaterThan(0);
}
当我运行测试时,我得到了错误
Newtonsoft.Json.JsonReaderException: Could not convert string to integer: 64fa643f-2d35-46e7-b3f8-31fa673d719b. Path '[0].id', line 1, position 45.
我也尝试过使用 webapi.client 包中的 ReadAsAsync() 方法,但仍然遇到同样的错误。
对我试图测试的端点的 api 调用会给出:
[
{
"id": "64fa643f-2d35-46e7-b3f8-31fa673d719b",
"title": "Garden Trimming in Lagos Ajah",
"description": "Please my garden needs trimmin, Im in lagos",
"initialPrice": 20.3
}
]
非常感谢您的帮助
【问题讨论】:
-
我有一些代码运行最新版本的 Newtonsoft (12),它反序列化 Guids 就好了。您使用的是旧版本的 NS 吗?
-
我看不出你的代码有什么问题,看起来它应该可以正常工作。这可能是 Json.NET 中的一个错误:github.com/dotnet/orleans/issues/1187
-
@BryanLewis 嗨,感谢您的意见,是的,我正在运行最新版本
标签: c# asp.net-web-api .net-core json.net