What are unit tests?

I am convinced of the usefulness of unit tests, but I really don't understand the rules for them.

If I have a class associated with another

public class MyClass
{       
    private SecondClass MySecondClass;

    public MyClass()
    {
        this.MySecondClass = new SecondClass ();
    }
}

the field is private, and Myclass has this method:

    public ThirdClass Get()
    {
        return this.MySecondClass.Get();
    } 

How can I check this? I guess I need to check if the method is suitable MyClass.get() MySecondClass.Get()! But I can not make fun of SecondClassand assign it to the first, because it is a private field. So I'm really curious how you can verify this.

thank

+3
source share
1 answer

You cannot easily unit test, because the instance is hard-coded. You can use the constructor installation, where you could mock it:

public class MyClass
{       
    private SecondClass _mySecondClass;

    public MyClass(SecondClass mySecondClass)
    {
        _mySecondClass = mySecondClass;
    }

    public ThirdClass Get()
    {
        return _mySecondClass.Get();
    } 
}

unit test , . . , , .

+4

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


All Articles