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 30369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:26:13+00:00 2026-05-10T13:26:13+00:00

If I have interface IFoo, and have several classes that implement it, what is

  • 0

If I have interface IFoo, and have several classes that implement it, what is the best/most elegant/cleverest way to test all those classes against the interface?

I’d like to reduce test code duplication, but still ‘stay true’ to the principles of Unit testing.

What would you consider best practice? I’m using NUnit, but I suppose examples from any Unit testing framework would be valid

  • 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. 2026-05-10T13:26:14+00:00Added an answer on May 10, 2026 at 1:26 pm

    If you have classes implement any one interface then they all need to implement the methods in that interface. In order to test these classes you need to create a unit test class for each of the classes.

    Lets go with a smarter route instead; if your goal is to avoid code and test code duplication you might want to create an abstract class instead that handles the recurring code.

    E.g. you have the following interface:

    public interface IFoo {      public void CommonCode();      public void SpecificCode();  } 

    You might want to create an abstract class:

    public abstract class AbstractFoo : IFoo {      public void CommonCode() {           SpecificCode();     }      public abstract void SpecificCode();  } 

    Testing that is easy; implement the abstract class in the test class either as an inner class:

    [TestFixture] public void TestClass {      private class TestFoo : AbstractFoo {         boolean hasCalledSpecificCode = false;         public void SpecificCode() {             hasCalledSpecificCode = true;         }     }      [Test]     public void testCommonCallsSpecificCode() {         TestFoo fooFighter = new TestFoo();         fooFighter.CommonCode();         Assert.That(fooFighter.hasCalledSpecificCode, Is.True());     } } 

    …or let the test class extend the abstract class itself if that fits your fancy.

    [TestFixture] public void TestClass : AbstractFoo {      boolean hasCalledSpecificCode;     public void specificCode() {         hasCalledSpecificCode = true;     }      [Test]     public void testCommonCallsSpecificCode() {         AbstractFoo fooFighter = this;         hasCalledSpecificCode = false;         fooFighter.CommonCode();         Assert.That(fooFighter.hasCalledSpecificCode, Is.True());     }          } 

    Having an abstract class take care of common code that an interface implies gives a much cleaner code design.

    I hope this makes sense to you.


    As a side note, this is a common design pattern called the Template Method pattern. In the above example, the template method is the CommonCode method and SpecificCode is called a stub or a hook. The idea is that anyone can extend behavior without the need to know the behind the scenes stuff.

    A lot of frameworks rely on this behavioral pattern, e.g. ASP.NET where you have to implement the hooks in a page or a user controls such as the generated Page_Load method which is called by the Load event, the template method calls the hooks behind the scenes. There are a lot more examples of this. Basically anything that you have to implement that is using the words ‘load’, ‘init’, or ‘render’ is called by a template method.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer http://www.census.gov/statab/www/ccdb.html Look in the 'Selected features...' section where there are… May 11, 2026 at 1:40 pm
  • added an answer Try gcc option -fdump-tree-fixupcfg-lineno. It will 'pretty print' parsing AST… May 11, 2026 at 1:40 pm
  • added an answer To be perfectly frank: it doesn't really matter. As long… May 11, 2026 at 1:40 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.