Timeline for In C++, how would one unit test a method that must throw an exception when a private array no longer has "space"?
Current License: CC BY-SA 4.0
12 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 24, 2020 at 14:37 | vote | accept | Sean Francis N. Ballais | ||
| Feb 24, 2020 at 14:21 | comment | added | Christian Hackl | This is just the usual area of conflict between testability and simplicity. If you want to keep the class simple and not expose its private parts, then forget about the unit test and test the behaviour at a higher level (i.e. create a realistic situation which will result in too many entities being created and see if the error handling behaves correctly). You will ultimately have to decide if the benefits of unit-testing this class are worth the additional complexity. | |
| Feb 24, 2020 at 10:38 | comment | added | Caleth | It doesn't make MAX_NUM_ENTITIES a part of EntityManager. It makes it a parameter of | |
| Feb 23, 2020 at 23:10 | answer | added | VoiceOfUnreason | timeline score: 4 | |
| Feb 23, 2020 at 10:49 | comment | added | Sean Francis N. Ballais | @pschill, your solution seems to be a viable one for my case. Thanks! It would require making MAX_NUM_ENTITIES a part of EntityManager though. But, I suppose that wouldn't be an issue. | |
| Feb 23, 2020 at 9:41 | comment | added | pschill | We had a similar problem in our company. We couldnt (didnt want to) move MAX_NUM_ENTITIES to a constructor argument, because then it would be fixed at runtime and not at compile time. Our solution was to move it to a template parameter and add a typedef for the original size: using DefaultEntityManager = EntityManager<10000>. This way, the unit tests could use EntityManager<10>. Would this work in your case? | |
| Feb 23, 2020 at 9:04 | comment | added | besc | How regularly is 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). | |
| Feb 23, 2020 at 8:59 | comment | added | user949300 | I'd consider changing MAX_NUM_ENTITIES from a const to a constructor parameter, perhaps with a default value. For your unit test pass a tiny number. | |
| Feb 23, 2020 at 5:00 | comment | added | Sean Francis N. Ballais | @BerinLoritsch, thanks for that suggestion. But, the method should only throw an exception when it has been called more than 10,000 (actually 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 the try/catch block 10,001 times just for the method to throw an exception? Or is there a better way? | |
| Feb 23, 2020 at 3:37 | comment | added | Berin Loritsch | I would call the test in a try/catch and put an explicit failure after the call within the try 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. | |
| Feb 23, 2020 at 3:16 | history | edited | Sean Francis N. Ballais | CC BY-SA 4.0 | Added more details. |
| Feb 23, 2020 at 3:09 | history | asked | Sean Francis N. Ballais | CC BY-SA 4.0 |