【问题标题】:Passing stream from test case to method将流从测试用例传递到方法
【发布时间】:2020-03-24 11:35:50
【问题描述】:

我正在为 API 编写测试用例。

在 API 中,我在 HttpRequest 的主体中获取内存流。在下面的 sn-p 中,reqHttpRequest 的一个实例

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 中的值作为空字符串获取。 请帮助我了解我哪里出错了。

【问题讨论】:

    标签: c# unit-testing stream moq


    【解决方案1】:

    JsonConvert 需要 json,但这里没有 json。

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      相关资源
      最近更新 更多