I have multiple classes that implement the same interface, how should I write unit tests to verify that each class implements the interface correctly, keeping code duplication to a minimum (DRY)?
As an example of what I mean, the following is a very basic library containing two implementations of IDeleter: Deleter1 and Deleter2. Both implement the method Delete by calling Delete on their associated IRepository.
using Microsoft.Practices.Unity;
namespace TestMultiple
{
public interface IRepository
{
void Delete(string id);
}
public abstract class Baseclass
{
protected abstract IRepository GenericRepository { get; }
public void Delete(string id)
{
GenericRepository.Delete(id);
}
}
public interface IDeleter
{
void Delete(string id);
}
public interface IRepository1 : IRepository
{
}
public abstract class RepositoryBase
{
public void Delete(string id)
{
}
}
public class Repository1 : RepositoryBase, IRepository1
{
}
public class Deleter1 : Baseclass, IDeleter
{
protected override IRepository GenericRepository { get { return Repository; } }
[Dependency]
public IRepository1 Repository { get; set; }
}
public interface IRepository2 : IRepository
{
}
public class Repository2 : RepositoryBase, IRepository2
{
}
public class Deleter2 : Baseclass, IDeleter
{
protected override IRepository GenericRepository { get { return Repository; } }
[Dependency]
public IRepository2 Repository { get; set; }
}
}
For these two classes, Deleter1 and Deleter2, I have written two corresponding unit test classes as shown in the below snippet. The tests check the same behaviour, i.e. that Delete is called on the underlying repository. Is there some better way to implement the same tests for all implementations of IDeleter? For example, should I write a baseclass containing common test methods, such as TestDelete, for TestDeleter1 and TestDeleter2?
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using Moq;
namespace TestMultiple.Tests
{
[TestClass]
public class TestDeleter1
{
[TestMethod]
public void TestDelete()
{
var mockRepo = new Mock<IRepository1>();
var container = new UnityContainer().RegisterInstance<IRepository1>(mockRepo.Object);
var deleter = container.Resolve<Deleter1>();
deleter.Delete("id");
mockRepo.Verify(r => r.Delete("id"));
}
}
[TestClass]
public class TestDeleter2
{
[TestMethod]
public void TestDelete()
{
var mockRepo = new Mock<IRepository2>();
var container = new UnityContainer().RegisterInstance<IRepository2>(mockRepo.Object);
var deleter = container.Resolve<Deleter2>();
deleter.Delete("id");
mockRepo.Verify(r => r.Delete("id"));
}
}
}
EDIT:
Feel free to mention unit test frameworks that may help solve this kind of problem, although my preference is with NUnit.
There’s no easy way to write tests in the frameworks I’m aware of that assert common behaviour on interfaces. The best you can do is write tests and helper methods as if you were testing an abstract class and then insert the real type into derived test classes.
For example, you could create an
DeleterTestsclass, which provides tests for the interface:Then, you inherit from this class for anything implementing
IDeleter, implementing the abstractCreateDeletermethod as you need to:If you needed to compose the instances differently, you could implement the abstract
CreateDeleterin any fashion.