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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:29:13+00:00 2026-05-18T00:29:13+00:00

So basically, I have an abstract class which has a unique, incremental ID –

  • 0

So basically, I have an abstract class which has a unique, incremental ID – Primitive. When a Primitive (or more precisely, an inheritor of Primitive) is instantiated, the ID is incremented – up to the point where the ID overflows – at which point, I add a message to the exception and rethrow.

OK, that all works fine… but I’m trying to test this functionality and I’ve never used mocking before. I just need to make enough Primitives for the ID to overflow and assert that it throws at the right time.

  • It is unreasonable to instantiate 2 billion objects to do this! However I don’t see another way.
  • I don’t know if I’m using mocking correctly? (I’m using Moq.)

Here’s my test (xUnit):

[Fact(DisplayName = "Test Primitive count limit")]
public void TestPrimitiveCountLimit()
{
    Assert.Throws(typeof(OverflowException), delegate()
    {
        for (; ; )
        {
            var mock = new Mock<Primitive>();
        }
    });
}

and:

public abstract class Primitive
{
    internal int Id { get; private set; }
    private static int? _previousId;

    protected Primitive()
    {
        try
        {
            _previousId = Id = checked (++_previousId) ?? 0;
        }
        catch (OverflowException ex)
        {
            throw new OverflowException("Cannot instantiate more than (int.MaxValue) unique primitives.", ex);
        }
    }
}

I assume I’m doing it wrong – so how do I test this properly?

  • 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-18T00:29:13+00:00Added an answer on May 18, 2026 at 12:29 am

    You don’t need mocking for this. You use mocking when two classes work together and you want to replace one class with a mock (fake) one so you only have to test the other one. This is not the case in your example.

    There is however a way you could use mocks, and that fixes your issue with the 2bln instances. If you separate the ID generation from the Primitive class and use a generator, you can mock the generator. An example:

    I’ve changed Primitive to use a provided generator. In this case it’s set to a static variable, and there are better ways, but as an example:

    public abstract class Primitive
    {
        internal static IPrimitiveIDGenerator Generator;
    
        protected Primitive()
        {
            Id = Generator.GetNext();
        }
    
        internal int Id { get; private set; }
    }
    
    public interface IPrimitiveIDGenerator
    {
        int GetNext();
    }
    
    public class PrimitiveIDGenerator : IPrimitiveIDGenerator
    {
        private int? _previousId;
    
        public int GetNext()
        {
            try
            {
                _previousId = checked(++_previousId) ?? 0;
    
                return _previousId.Value;
            }
            catch (OverflowException ex)
            {
                throw new OverflowException("Cannot instantiate more than (int.MaxValue) unique primitives.", ex);
            }
        }
    }
    

    Then, your test case becomes:

    [Fact(DisplayName = "Test Primitive count limit")]
    public void TestPrimitiveCountLimit()
    {
        Assert.Throws(typeof(OverflowException), delegate()
        {
            var generator = new PrimitiveIDGenerator();
    
            for (; ; )
            {
                generator.GetNext();
            }
        });
    }
    

    This will run a lot faster and now you’re only testing whether the ID generator works.

    Now, when you e.g. want to test that creating a new primitive actually asks for the ID, you could try the following:

    public void Does_primitive_ask_for_an_ID()
    {
        var generator = new Mock<IPrimitiveIDGenerator>();
    
        // Set the expectations on the mock so that it checks that
        // GetNext is called. How depends on what mock framework you're using.
    
        Primitive.Generator = generator;
    
        new ChildOfPrimitive();
    }
    

    Now you have separated the different concerns and can test them separately.

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

Sidebar

Related Questions

I have created an abstract base class Animal which has public virtual abstract method
I basically have a page which shows a processing screen which has been flushed
I have a base class that has an abstract getType() method. I want subclasses
Basically I have the following class: class StateMachine { ... StateMethod stateA(); StateMethod stateB();
I have, for my game, a Packet class, which represents network packet and consists
I have an interface: public abstract class Authorizer<T> where T : RequiresAuthorization { public
I try to achive the opposite of here . I have an abstract class,
I have got an abstract base class and inherited poco entities. I am using
Basically I have a Parent superclass which i.e is called MAMMAL. The mammal by
Hi is it possible to cast a List? i have an abstract class that

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.