I am implementing an ECS system with a data-oriented design, and with a TDD methodology using Catch 2. I have the following class declaration snippet for an EntityManager
:
using Entity = std::uint16_t;
using Signature = std::bitset<MAX_NUM_COMPONENTS>;
const std::uint16_t MAX_NUM_ENTITIES = 10000;
class EntityManager
{
public:
...
Entity createEntity();
...
private:
Signature entities[MAX_NUM_ENTITIES];
...
}
The purpose of EntityManager::createEntity()
is to simply return an Entity
(which we will refer to as e
), and set entities[e]
to some value. However, the method must throw an exception when the number of entities that exists are more than MAX_NUM_ENTITIES
.
I would like to unit test the aforementioned method. But, I am unsure how to do so. An idea I have is to fill up the entire entities
array by calling createEntity()
MAX_NUM_ENTITIES + 1
times in my test to trigger an exception. However, I have a feeling that it is not the best way to do it, and may even result in a slow test run. Another idea, but similar to the previous one, is to somehow mock entities
so that it would have a small size. We will then call createEntity()
multiple times until an exception is raised just like the previous idea.
What approach would you recommend?
try/catch
and put an explicit failure after the call within thetry
block. The catch can either test the content of the exception or simply it's presence is the pass case. That's what languages like Java and C# did before they had lambdas or annotations. Works well.MAX_NUM_ENTITIES
, but we'll use its actual value instead for simplicity) times (without any prior entity deletions). Would it be wise to call the method inside thetry/catch
block 10,001 times just for the method to throw an exception? Or is there a better way?createEntity()
expected to change? If the answer is never or hardly ever, consider manual testing. For a stable piece of code an automated test that runs all the time doesn’t provide a lot of value, especially if the test is expensive (in runtime or increased test implementation complexity because of a mock).using DefaultEntityManager = EntityManager<10000>
. This way, the unit tests could useEntityManager<10>
. Would this work in your case?