Question

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 frameworks will only allow you to mock an interface.

Is there any way to mock a call to a provider that inherits from providerbase?

If not, is there a pattern that I can use to implement mocking of providers?

Was it helpful?

Solution

Mocking frameworks should be able to create for you a mock object based on a class, as long as it's got virtual members.

You may also want to take a look at Typemock

OTHER TIPS

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.

RhinoMocks or Moq will create test doubles for classes as well as for interfaces. The type has to have virtual methods or be abstract though. The Typemock isolator gets around this.

I'd suggest that the objects you want to mock probably should be abstract (dependency inversion principle).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top