How to check a function that has no return value?

I had a visual studio creating a test for each participant in my class. Here is one example:

/// <summary> ///A test for CloseCurrentTextLogFile ///</summary> [TestMethod()] public void CloseCurrentTextLogFileTest() { Logger.CloseCurrentTextLogFile(); Assert.Inconclusive( "A method that does not return a value cannot be verified." ); } 

Based on the assert line, I am wondering how to check this ... Any ideas?

+6
source share
5 answers

Static state methods naturally make themselves rather unstable, so my suggestion is based on reorganizing your code from static methods.

I would turn Logger into an instance class that accepts an IO object in the constructor. This will allow you to stub the I / O object, and you can state that the method of your I / O object Close was called.

This is only if you want to make your code 100% available for testing. Otherwise, I would agree with Mo that if it is not subject to verification, then do not write a forced test ... they are usually very fragile. In the end, you need to be pragmatic about your code. The registrar is often useful for maintaining static, however, as I mentioned, they are usually very unstable ... so just be pragmatic in your work and don’t write tests in a simple attempt to get 100% coverage of the code., That 100 % will come with a price ...

UPDATE

This is why this cannot be verified based on dogmatic POV unit testing. You are not testing the unit of work, but instead you are checking the Logger AND Logger dependencies (in this case, the IO object). It also slows down your tests and requires setting up the environment and state (you must first open the actual file to close it, right?). All this is bad for unit testing, but fine for integration testing ... so it depends on which tests you also write.

+3
source

I would say that if it is really not tested, then it actually does nothing and should not exist;) Something like this might work ...

 Assert.IsNotNull( Logger.File ); Logger.CloseCurrentTextLogFile(); Assert.IsNull( Logger.File ); 

Either check the status of Logger.FileOpenStatus or make sure that Logger.OpenFile(fname) throws an exception before closing, but not after. There should be something in Logger whose behavior depends on any CloseCurrentTextLogFile() action.

+3
source

You can check the status of Logger or you could call some other method in the log that would not result in an error because you called this method, which would be successful if you did not call the method.

+2
source

Not sure, but you can try the following: The function should do something (write a file, set some variables, etc.) Perhaps you can check if the variables were written or the file was created.

+2
source

You can mock the Logger class and claim that CloseCurrentTextLogFile is CloseCurrentTextLogFile . Some may argue that you need to check that all open log files are closed, I personally disagree with this, as this will check Logger itself is not your method. These are the questions that developers should ask themselves when they start developing their systems, how can I test my application.

+1
source

Source: https://habr.com/ru/post/919876/


All Articles