简体   繁体   中英

unit testing a file writer function

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

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.

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 to much. Separate xml writing functions into its 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.

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.

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;

Assert.IsTrue(File.Exists(filePath));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM