Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:15:46+00:00 2026-06-14T13:15:46+00:00

I have multiple classes that implement the same interface, how should I write unit

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:15:47+00:00Added an answer on June 14, 2026 at 1:15 pm

    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 DeleterTests class, which provides tests for the interface:

    public abstract class DeleterTests<TRepository> where TRepository : IRepository
    {
        [TestMethod]
        public void TestDelete()
        {
            var mockRepo = new Mock<TRepository>();
            var container = new UnityContainer();
    
            container.RegisterInstance<TRepository>(mockRepo.Object);
    
            var deleter = this.CreateDeleter(container);
    
            deleter.Delete("id");
            mockRepo.Verify(r => r.Delete("id"));
        }
    
        protected abstract IDeleter CreateDeleter(IUnityContainer container);
    }
    

    Then, you inherit from this class for anything implementing IDeleter, implementing the abstract CreateDeleter method as you need to:

    public class Deleter1Tests : DeleterTests<IRepository1>
    {
        protected override IDeleter CreateDeleter(IUnityContainer container)
        {
            return container.Resolve<Deleter1>();
        }
    }
    
    public class Deleter2Tests: DeleterTests<IRepository2>
    {
        protected override IDeleter CreateDeleter(IUnityContainer container)
        {
            return container.Resolve<Deleter2>();
        }
    }
    

    If you needed to compose the instances differently, you could implement the abstract CreateDeleter in any fashion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have multiple classes that inherit/extends same class in Google AppEngine
The situation: I have multiple classes that should each hold a variable with a
In Django, when you have a parent class and multiple child classes that inherit
I have multiple classes that will store similar data for different uses and I
I have multiple classes in multiple dll's and each dll's may include other. I
Is it a good practice to have multiple XXX : DbContext classes for each
I have a function - myFunc() in class A. There are multiple other classes
Possible Duplicate: Multiple Inheritance in C# I have two classes Class A and Class
Say we have a class inheriting from two base classes (multiple inheritance). Base class
I have a service that I want to inject into multiple client classes. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.