对文件编写器功能进行单元测试
我有一个函数,它将学生详细信息作为输入并将他们的成绩单写入 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.");
}
预先感谢,
问候, 约翰
I have a function which will take student details as input and write their report card into a xml file. When i tried to create unit tests in vs2008 i saw the message - "A method that does not return a value cannot be verified." My function does not return a value it merely writes into a file and returns.
How do i write tests for this function?
[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.");
}
Thanks in advance,
Regards,
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过良好的职责分离,可以很容易地用内存流之类的东西替换你的文件。写入内存流而不是文件。然后你可以针对其中的内容进行测试。但正如其他人提到的,代码示例可能会揭示其他需求。
更新:
感谢您的代码。看起来您的 StudentSettings 类做得太多了。将 XML 编写函数分离到它们自己的类中,从中提取一个接口,并将其作为构造函数参数注入到您的类中。然后您可以在测试期间将其替换为您自己的模拟。
With good separation of responsibilities, it would be easy to replace your file with something like a memorystream. Write into a memory stream instead of a file. Then you can test against the content of that. But as mentioned by others, a code example would maybe reveal other needs.
UPDATE:
Thanks for the code. It looks like your StudentSettings class is doing too much. Separate XML-writing functions into their own class, extract an interface from it, and inject this into your class as a constructor argument. Then you can replace it with your own mock during tests.
首先,如果Visual Studio大叔告诉你你的方法无法测试,那不一定是真的。
您应该以字符串形式返回要写入文件的输出,或者您的方法应该采用 TextWriter 作为参数。在前一种情况下,您可以使用模拟框架(如其他答案中所述)为测试中的方法提供一个假的 TextWriter 对象。
First of all, if the big uncle Visual Studio tells you that your method cannot be tested, it does not have to be true.
You should return the output to be written in the file as a string, or your method should take TextWriter as a parameter. In the former case you may use mocking framework, as mentioned in the other answer, to give the method under test a fake TextWriter object.
您可以使用模拟框架来执行此操作。 Moq 就是一个很好的例子。简而言之,您可以创建假对象,然后告诉它们表现得像另一个对象。您还可以验证方法的调用频率、是否被调用以及应该调用的频率。
编辑:
此处显示的快速入门指南提供了一些很好的示例,可能会让您处于正确的位置直接的。在您的情况下,您可以创建包含写入文件的函数的类的最小起订量。使用验证函数,您可以检查/验证该函数的调用频率以及它的运行是否没有任何异常。
You can use an mocking framework to do this. An good example is Moq. Simply put you can create fake-objects and you tell them to behave like another. You can also verify how often and method is called, if it is called and how often it should be called.
EDIT:
The quick startguide shown here has some good examples which probably will put you in the right direct. In your case you could create an moq of your class containing the function which writes your file. Using the verify function you can check/verify how often the function is called and if it runs without any exceptions.
代码生成器只是表明它无法根据其返回值(void)来验证您的测试,这是完全有道理的。我认为其他人提到这更像是一个占位符。在实际编写测试时,您需要确定真正的通过标准是什么。您可以像这样简单地进行:
如果您关心的只是现有文件,或者您可以更深入地了解它,验证其内容等等。这真的取决于你。
The code generator is merely suggesting that it can't verify your test based on its return value (void) which makes perfect sense. I think someone else mentioned that this is more of a placeholder. When it comes to actually writing the test, you need to decide what your passing criteria really is. You can go as easy as;
If all you care about is the file existing, or you can get deeper down into it, verify its contents and so forth. It is really up to you.