gpt4 book ai didi

c# - 断言 JSON JArray Api 响应

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:30 25 4
gpt4 key购买 nike

断言 JArray Json 响应时,出现错误 Assert.AreEqual failed. Expected:<Newtonsoft.Json.Linq.JArray[,] Newtonsoft.Json.Linq.JArray[,])>. Actual:<[ 1, 3]

你能告诉我代码中的错误在哪里吗?我认为问题在于 ResponseReceived类。


dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);
var responseTarget = jsonResponse.target;

//create a new response expected object
var responseExpected = new ResponseExpected();

//create a new post response received object
var responseReceived = new ResponseReceived();
responseReceived.Target = responseTarget;

//Assert
Assert.AreEqual(responseExpected.Target, responseReceived.Target);


public class ResponseReceived
{
public JArray Target{ get; set; }

}

public class ResponseExpected
{
public JArray[,] Target{ get; set; } = new JArray[1, 3];
}
//post request json
var myObject = new {
target = new []{ 5, 5 }
}

更新:该问题已使用 public JArray Target{ get; set; } = new JArray(new[] { 1, 3 }); 解决

但是这似乎适用于除 MSTest 之外的大多数测试框架。 MSTest 如何解决这个问题?

最佳答案

您不能指望单个 JArray 会等于 JArray 的数组假设您的 json 看起来类似于

{
"target": [
5,
5
]
}

你可以这样写断言:

var input = "{\r\n  \"target\": [\r\n    5,\r\n    5\r\n  ]\r\n}";
dynamic json = JsonConvert.DeserializeObject(input);

var expected = new JArray(new[] {1, 3}); // not "new JArray[1, 3];"
var actual = json.target;

Assert.AreEqual(expected, actual);

关于c# - 断言 JSON JArray Api 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55748853/

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