Mocking classes that aren't interfaces

前端 未结 3 1090
无人共我
无人共我 2020-12-11 17:37

I\'ve been writing some providers in c# that inherit from the providerbase class. I\'ve found that it\'s hard to write tests that use the providers as most mocking framework

3条回答
  •  爱一瞬间的悲伤
    2020-12-11 18:04

    I know Rhino mocks can mock classes too, most other mocking frameworks should have no problems with this either.
    Things too keep in mind: The class can't be sealed. You need to mark methods you want to mock virtual and the class needs a constructor with no arguments this can be protected, private won't work. (just tried this out)

    Keep in mind that the mocking framework will just create a class that inherits from your class and creates an object of that type. So constructors will get called. This can cause unexpected behaviour in your tests.

提交回复
热议问题