views:

343

answers:

2

I am using Rhino mocks for unit test mocking of objects. Our DAL uses codesmith to generate code from .netTiers templates, which creates these DataRepository classes that contain all the methods for CRUD type transaction to the datasource. In unit testing, I am trying to mock this data repository object which has no interface class to use as a stub.

In Brief, has anyone successfully used Rhino mocks to mock a .netTiers generated DataRepository, avoiding the need for a test database and real transactions against the datasource that needs to be tore down at the end??

A: 

In short, Rhino can only mock types that are either interfaces or non-sealed classes. And then, you can only stub methods that are virtual or abstract.

If your goal is to substitute a DataRepository with a mock implementation I think you will have to look into more advanced mocking frameworks like TypeMock.

If you're in control of the CS templates, another option would be to use the templates to also generate mock DataRepository implementations.

Peter Lillevold
+2  A: 

I've been pondering this one for a while, as no one has stepped up and written a mock DataRepository implementation for netTiers yet (to my knowledge).

As I don't care too much for the record-replay steps of TypeMock and RhinoMocks, I've opted for the newer Moq, which will happily mock either the DataRepository classes or the Service layer calls themselves with minimal hassle.

Assuming you're on .NET 3.5, I'd recommend it.

Soundwave