【发布时间】:2016-01-19 09:47:44
【问题描述】:
我目前有这样的方法
public void BusinessMethod(object value, StreamWriter sw)
{
//Calls a private method that converts the data in `value` to a custom formatted string variable `str`
string str = myPrivateMethod(value);
//write the string to stream
sw.Write(str);
}
我正在尝试使用here 提到的方法测试此方法,并且做了完全相同的事情。但是,我的 result 字符串作为空字符串返回。我无法更改方法签名。如何测试这样的方法?我正在使用 Nunit 进行测试。
这是我的测试方法
[Test]
public void My_Test()
{
MyPoco dto = new MyPoco ();
//set up the dto properties here
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
sut.BusinessMethod(dto, writer);
string result = Encoding.UTF8.GetString(stream.ToArray());
}
}
【问题讨论】:
-
您尝试验证的确切行为是什么?
-
我正在尝试验证发送到
BusinessMethod的 dto 是否被写入流。这就是BusinessMethod的全部责任。 -
由于您的测试没有断言,它实际上并没有测试任何东西。能否提供一个包含所有相关代码的最小复现案例?
-
您还提到
actual是空的,但我没有看到和实际。你的意思是result?此外,您还需要 Assert.AreEqual("Expected Result", result); -
在从流中读取数据之前尝试刷新流写入器。
标签: c# .net unit-testing nunit automated-tests