Search code examples
c#visual-studio-2008unit-testing

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


Solution

  • 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.