¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

[TDD] a Vehicule_ForTests subclass for the Vehicule class ?


 

There are cases where I may subclass a class under test in order to
instrument
it for testing, but I find it needs to be done very carefully. It's quite
easy to
create a test that _only_ passes with the subclass and not the original
class
if you add too much logic in the subclass.

If by "sugar" you mean an method that makes it easier to do something that
you can already do, but which requires several lines of code, I'd say "Don't
do that!" Add the code right in your test. After you have added it twice,
move it to a method.

If you want to add behavior to the object under test, then you may have to
subclass it. Be careful, however, not to add behavior that changes the
state of the object. I use subclasses, for example, to save a record of
actions taken by the original object, over which I can later assert.

IOW, I think the answer to your question depends on what it is you
are planning to do in the subclass.

Charlie

On Fri, Aug 31, 2012 at 10:06 AM, pierre masci <piemas25@...> wrote:

**


Is it a good or bad idea to subclass the Vehicule class with the
Vehicule_ForTests class,
which would contain all the "sugar" methods that i use to test the Vehicule
class ?

It seems good, to not add useless methods to the public API...
but one could say that i can obtain the same result by starting my "sugar
for tests" methods with "_T" for instance.
It still feels nice to have these functions in a separate place.

Could it be dangerous or uncomfortable, too ?





[Non-text portions of this message have been removed]