How to mock with static methods?

后端 未结 7 1093
星月不相逢
星月不相逢 2020-12-05 02:31

I\'m new to mock objects, but I understand that I need to have my classes implement interfaces in order to mock them.

The problem I\'m having is that in my data acce

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 03:07

    You might be trying to test at too deep a starting point. A test does not need to be created to test each and every method individually; private and static methods should be tested by calling the public methods that then call the private and static ones in turn.

    So lets say your code is like this:

    public object GetData()
    {
     object obj1 = GetDataFromWherever();
     object obj2 = TransformData(obj1);
     return obj2;
    } 
    private static object TransformData(object obj)
    {
    //Do whatever
    }
    

    You do not need to write a test against the TransformData method (and you can't). Instead write a test for the GetData method that tests the work done in TransformData.

提交回复
热议问题