Legacy Unit Testing C # Code

How to write a NUnit test for such a method. Does this method have refactoring? What is the best approach for considering scenarios like this in leagacy code?

         public bool DoXYZ()
            {
                ABC abc= new ABC()
                XYZ xyz = new XYZ();
                if (xyz .IsSomeCondition(Session.SessionID))
                { return false; }
                else
                { return abc.IsSomeOtherCondition(SessionID.SessionID); }
            }
+3
source share
4 answers

You may have to reorganize it to introduce hooks for dependency injection. For example, a class containing the DoXYZ method can get new properties for ABC and XYZ. These properties can be used by default for ABC and XYZ instances, but in unit tests they can be replaced with mock versions.

And if you prefer to use IoC, this approach also supports this.

+3
source

- .

? , sessionid.

, (short), -, IoC (. ).

+1

:

  • , . , .
  • .

, , .

MSDN.

+1

,

  • 2 - 2 .
  • . ctor/method. (, make xyz.IsSomeCondition false ). ABC XYZ - , , . .
  • extract sessionId () , ,

.

public bool DoXYZ(ABC abc, XYZ xyz, Guid sessionId) 
{    if (xyz.IsSomeCondition(sessionId))    
        return false; 

     return abc.IsSomeOtherCondition(sessionId);  
}
0

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


All Articles