将流从测试用例传递到方法
Passing stream from test case to method
我正在为 API.
编写测试用例
在 API 中,我正在获取 HttpRequest 主体中的内存流。在下面的代码片段中,req
是 HttpRequest
的一个实例
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
所以,在模拟时我有这个代码:
Data data = new Data()
{
width = 400,
height = 600,
text = "text",
barcodeformat = ZXing.BarcodeFormat.CODE_128.ToString()
};
byte[] inputData = null;
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, data);
inputData = ms.ToArray();
}
var mockHttp = new Mock<HttpRequest>();
mockHttp.Setup(x => x.Body).Returns(new MemoryStream(inputData));
但是在 API 代码中,我将 data
中的值作为空字符串获取。
请帮助我了解我哪里出错了。
JsonConvert
需要 json,但你这里没有 json。
我正在为 API.
编写测试用例在 API 中,我正在获取 HttpRequest 主体中的内存流。在下面的代码片段中,req
是 HttpRequest
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
所以,在模拟时我有这个代码:
Data data = new Data()
{
width = 400,
height = 600,
text = "text",
barcodeformat = ZXing.BarcodeFormat.CODE_128.ToString()
};
byte[] inputData = null;
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, data);
inputData = ms.ToArray();
}
var mockHttp = new Mock<HttpRequest>();
mockHttp.Setup(x => x.Body).Returns(new MemoryStream(inputData));
但是在 API 代码中,我将 data
中的值作为空字符串获取。
请帮助我了解我哪里出错了。
JsonConvert
需要 json,但你这里没有 json。