unidad de prueba de una función de escritura de archivos

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?

public void StoreInformationTest()
   StudentSettings target = new StudentSettings(); // TODO: Initialize to an appropriate   
   StudentSettings settings = null; // TODO: Initialize to an appropriate value
   Assert.Inconclusive("A method that does not return a value cannot be verified.");

Gracias por adelantado,

Saludos, John

preguntado el 16 de mayo de 11 a las 17:05

Post some code otherwise it's hard to know what you're doing. -

I am not mistaken, the code generated by Visual Studio when creating unit tests includes an Inconclusive statement as a placeholder. So if you think that it is not of problem to check return value, you can just comment out Assert.Inconclusive() statement. On a side note, I have been using Nunit and that worked great as well. -

added code but dont know how to bring the VS format here. I added four spaces in the beginning. Thank You. -

Like I commented, if return value is not what you need to test, you can just comment it out. Look at msdn.microsoft.com/en-us/library/ms182532.aspx as an example. They also comment that out. -

4 Respuestas

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.

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.

contestado el 16 de mayo de 11 a las 22:05

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.

contestado el 16 de mayo de 11 a las 22:05

+1.... but note that if you are handling files, then it is an integration test rather than a unit test - Morten

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 aquí 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.

contestado el 16 de mayo de 11 a las 22:05

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.

contestado el 16 de mayo de 11 a las 22:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.