cel*_*rno 5 c# unit-testing anonymous-types httpclient nsubstitute
我正在使用 HttpClient 的包装器来模拟 http 响应。我对那些发送匿名类型的人有疑问。
这是我的 HttpClient 类包装器的设置响应:
var x = new {category = 1, products = "1"};
Func<HttpRequestMessage, HttpResponseMessage> functionResponse;
functionResponse = request =>
request.CreateResponse(code, responseValue, _httpConfig);
_client.PostAsJsonAsync(
Arg.Is<string>(x => x.Contains(URL)),
Arg.Is<T>(x => x.ToJsonStringSafe() == postValue.ToJsonStringSafe())
).Returns(
System.Threading.Tasks.Task.Factory.StartNew(() =>
functionResponse(_message)));
Run Code Online (Sandbox Code Playgroud)
这是执行 httpClient 调用的目标方法中的一段代码:
var response =
await _httpClient.PostAsJsonAsync(
_serviceUrl + "/productbyid",
new {category = _category, products = _products});
Run Code Online (Sandbox Code Playgroud)
此时,response为null,但应该是一个带有responseValueset的Task。
此配置适用于除匿名类型之外的任何类型。
*我在调试时验证了 URL、类别和 _products 的匹配。