Building layouts in unit tests

Is there a way to create a layout instead of a real instance when testing code that calls the constructor?

For instance:

public class ClassToTest
{
  public void MethodToTest()
  {
    MyObject foo = new MyObject();
    Console.WriteLine(foo.ToString());
  }
}

In this example, I need to create a unit test that confirms that a call to the MethodToTest method on an instance of ClassToTest really outputs the result of the ToString () method of the newly created instance of MyObject.

I do not see a way to realistically test the ClassToTest class in isolation; testing this method will actually test the myObject.ToString () method as well as the MethodToTest method.

+3
source share
1 answer

. ClassToTest - . , , , - , . :

public class ClassToTest
{
  MyObject _foo;
  public void MethodToTest(Myobject foo)
  {
    _foo = foo;
    Console.WriteLine(foo.ToString());
  }
}

:

public class ClassToTest
{
  public MyObject Foo { get; set; }
  public void MethodToTest()
  {
  }
}

, ; , , .

, .

+3

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


All Articles