views:

34

answers:

1

Hi all

I have a bunch of repository classes which I want to unit-test using Visual Studio 2008. They implement the following interface:

public interface IRepository<TEntity> where TEntity : IEntity
{
    /// <summary>
    /// Get entity by ID
    /// </summary>
    /// <param name="id">The ID</param>
    /// <returns></returns>
    TEntity GetById(int id);

    /// <summary>
    /// Get all entities
    /// </summary>
    /// <returns></returns>
    IEnumerable<TEntity> GetAll();
}

Now I could write a fully blown test class for each and every repository I have. However to minimize redundancies, I want to write a base test class which contains the main "generic" test methods. This would allow me to write a simple subclass for every repository, like this:

[TestClass]
public class EmployeeRepositoryTest : RepositoryTestBase<Employee>
{
     // all test methods are inherited from the base class
     // additional tests could be added here...
}

However, the test methods defined in RepositoryTestBase are not detected by Visual Studio (because of generics), making this approach useless. To make it work, I would need to wrap each method of the base class to make them visible to Visual Studio, which causes redundancies again..

Is there a better approach to solve this pain? I don't want to clutter my tests with tons of wrapping code :(

+1  A: 

Is it possible to not have generics in the testing declaration, instead relying on IEntity alone, like:

IEntity GetById(int id);
IEnumerable<IEntity> GetAll();

And if those two functions need to do any instantiation of IEntity, you could have the Repository constructor accept a factory object. It would be the factory object's job to do the instantiation. There would be an abstract base (or interface) for the factory whose instantiation method just returns an IEntity, and child classes that may or may not be templated.

Reinderien