我有一个函数可以将学生详细信息作为输入并将他们的成绩单写入 xml 文件。当我尝试在 vs2008 中创建单元测试时,我看到消息 - “无法验证不返回值的方法。”我的函数不返回值,它只是写入文件并返回。
如何为这个函数编写测试?
[TestMethod()]
public void StoreInformationTest()
{
StudentSettings target = new StudentSettings(); // TODO: Initialize to an appropriate
StudentSettings settings = null; // TODO: Initialize to an appropriate value
target.StoreInformation(settings);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
提前致谢
问候, 约翰
最佳答案
有了很好的职责分离,就可以很容易地用内存流之类的东西替换你的文件。写入内存流而不是文件。然后你可以测试它的内容。但正如其他人所提到的,代码示例可能会揭示其他需求。
更新:
感谢您的代码。看起来您的 StudentSettings 类正在做很多事情。将 xml 编写函数分离到它自己的类中,从中提取一个接口(interface),并将其作为构造函数参数注入(inject)到您的类中。然后你可以在测试期间用你自己的模拟替换它。
关于c# - 单元测试文件编写器功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6021363/