Mocking Generic Method with NSubstitute

后端 未结 1 554
广开言路
广开言路 2021-01-13 05:12

I have an interface with a number of generic methods. These methods perform operations based on the type of data that is passed in. How do I mock this with NSubstitute? At t

相关标签:
1条回答
  • 2021-01-13 05:43

    NSubstitute doesn't support setting up multiple instances of a generic method automatically.

    The way we'd normally see IInstanceSource used in a test is to configure it for a specific bit of code under test, so T would be known. If a single fixture needed to work for a few different Ts, we could make configuration simpler by having a helper method like ConfigureInstanceSource<T>() which would do the configurations steps for a specific T.

    In your case though it seems like you want a fixed behaviour for all fake instances of IInstanceSource, in which case I believe you are going the right way about it by hand-coding your own test double.

    0 讨论(0)
提交回复
热议问题