0

我想从我的 REST API 接收 JSON 并将其转换为 POCO。它应该很简单,但事实证明并非如此:(

在我的单元测试中,我有一个 API 发送的示例 JSON 数据字符串:

string mockJsonResponse = @"[{
                ""project_name"": ""Mailjet Support"",
                ""cluster_name"": ""24/7 Support"",
                ""is_billable"": ""1"",
                ""usedtime"": ""128""
            },
            {
                ""project_name"": ""Caring"",
                ""cluster_name"": ""Caring"",
                ""is_billable"": ""0"",
                ""usedtime"": ""320""
            },
            {
                ""project_name"": ""Engagement"",
                ""cluster_name"": ""Community"",
                ""is_billable"": ""0"",
                ""usedtime"": ""8""
            }]";

我通过 HttpTest 从测试发送到我的代码:

httpTest.RespondWithJson(mockJsonResponse);

我试图在我的代码中接收它:

dynamic response = "http://api.com".GetJsonListAsync();

但它总是在测试资源管理器中失败并出现一个非常普遍的错误:

Result Message: Flurl.Http.FlurlHttpException : Request to http://api.com failed.

进一步挖掘它似乎无法将字符串序列化为 poco。我尝试过直接使用上面的字符串变量进行手动序列化,它很容易转换为我的模型类,所以它不可能是代码结构问题。

// same string variable above
var jsons = JsonConvert.DeserializeObject<List<Model>>(mockJsonResponse); // this runs fine

所有这些都失败了:

dynamic response = await "http://www.api.com".GetJsonAsync();
dynamic response = await "http://www.api.com".GetJsonAsync<Model>();
var response = await "http://www.api.com".GetJsonAsync<Model>();    
IList<dynamic> response = await "http://www.api.com".GetJsonListAsync();

模型类:

public class Model
{
    public string project_name { get; set; }
    public string cluster_name { get; set; }
    public string is_billable { get; set; }
    public string usedtime { get; set; }
}

编辑 我尝试将它作为一个带有 的字符串GetStringAsync,并且似乎该字符串以某种方式被破坏了。传递给的这个字符串JsonConvert.Deserialize<Model>()将无法通过测试。这就是 Visual Studio 调试器显示的内容。有很多转义字符。

在 Visual Studio 调试器中从 HttpTest 接收到的字符串的屏幕截图

4

2 回答 2

1

在尝试手动模拟 json 时,您并没有使用格式良好的 JSON。

我建议创建一个集合,对其进行序列化并将其作为示例 JSON 返回。

Model[] models = new []{
    new Model {
        project_name = "Mailjet Support",
        cluster_name = "24/7 Support",
        is_billable = "1",
        usedtime = "128"
    },               
    new Model{                
        project_name = "Caring",
        cluster_name = "Caring",
        is_billable = "0",
        usedtime = "320"
    },             
    new Model{               
        project_name = "Engagement",
        cluster_name = "Community",
        is_billable = "0",
        usedtime = "8"
    }
};

string mockJsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(models);
于 2016-06-29T11:17:53.150 回答
1

RespondWithJson获取一个将为您序列化为 JSON 的对象,而不是已经序列化的字符串。用匿名对象表示测试响应,你应该很好:

var mockJsonResponse = new[] {
    new {
        project_name = "Mailjet Support",
        cluster_name = "24/7 Support",
        is_billable = "1",
        usedtime = "128"
    },               
    new {                
        project_name = "Caring",
        cluster_name = "Caring",
        is_billable = "0",
        usedtime = "320"
    },             
    new {               
        project_name = "Engagement",
        cluster_name = "Community",
        is_billable = "0",
        usedtime = "8"
    }
};

httpTest.RespondWithJson(mockJsonResponse);
于 2016-06-29T23:13:18.817 回答