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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:14:55+00:00 2026-05-17T23:14:55+00:00

What would be the proper way to unit test an abstract class which takes

  • 0

What would be the proper way to unit test an abstract class which takes generic type parameter where the type is also an abstract class? E.g)

public abstract class BaseClass<T> where T : BaseT { ... }

I’ll have to create a test class which extends BaseT, right??

and what happen if the BaseClass support method chaining such that

public abstract class BaseClass<T> where T : BaseClass<T> { ... } ??

thanks.

updated concrete class sample for the 2nd case:

    public class ConcreteClass : BaseClass<ConcreteClass>
    {
        public ConcreteClass Method1()
        {
            return this as ConcreteClass;
        }
    }
  • 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-05-17T23:14:56+00:00Added an answer on May 17, 2026 at 11:14 pm

    You can do this without implementing a dummy implementation class using Rhino Mocks: http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx

    Unfortunately, Rhino Mocks requires you to use the record/playback syntax with partial mocks instead of the AAA syntax.

    Given the following abstract classes:

    public abstract class BaseAbstract
    {
        public abstract int Increment();
    }
    
    public abstract class AbstractClass<T> where T : BaseAbstract
    {
        private int inc;
    
        public abstract T Abstract();
    
        public virtual int Concrete()
        {
            return inc += Abstract().Increment();
        }
    }
    

    You can test the implementation of Concrete in the following way:

    [Test]
    public void mock_an_abstract_class_with_generic()
    {
        var mocks = new MockRepository();
        var baseAbstract = mocks.StrictMock<BaseAbstract>();
        var abstractClass = mocks.PartialMock<AbstractClass<BaseAbstract>>();
    
        using (mocks.Record())
        {
            baseAbstract.Stub(a => a.Increment()).Return(5);
            abstractClass.Stub(a => a.Abstract()).Return(baseAbstract);
        }
    
        using (mocks.Playback())
        {
            abstractClass.Concrete().ShouldEqual(5);
            abstractClass.Concrete().ShouldEqual(10);
        }
    }
    

    In essence, you create a partial mock for the base class, set expectations on the abstract methods, then call the concrete method under test. The generic is simply another mock, another partial if necessary.

    The downside of this is obviously the requirement for the record/playback syntax. I don’t know if other mocking frameworks can help you out more here, but I’ve generally found that Rhino Mocks is the mocking framework you go to if you have an advanced use case like this.

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

Sidebar

Related Questions

For a file containing the given class, SomeCoolClass, what would be the proper or
I would like to clarify what is the proper way to filter user input
Would the following SQL remove also the index - or does it have to
In a C++ application, let's say I have a window class, which has several
Would it not make sense to support a set of languages (Java, Python, Ruby,
Would it be possible to print Hello twice using single condition ? if condition
Would it be possible to show an image in full screen mode using silverlight.
Would having a nice little feature that makes it quicker to write code like
Would it be wwwroot, C, the root virtual directory where the assets are hosted,
Would a C++ CLI compiler be able to compile some large sets of C++

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.