Moq fake one method but use real implementation of another

前端 未结 2 1168
北荒
北荒 2021-02-06 21:31

Given an interface IService that has Method1() and Method2().

I want to test that when Method1() throws an Exce

2条回答
  •  忘掉有多难
    2021-02-06 21:50

    You can do this with:

    var mock = new Mock(){ CallBase = true };
    mock.Setup(m => m.Method1....
    

    The above code will use the real implementation of MyNetworkStream for any method/property which is not explicitly setup. I.e. it'll call the real Method2(), while the Method1() will be the mocked version.

    CallBase=true is usually meant to test abstract classes (if this is right or wrong, is out of the scope of this question).

提交回复
热议问题