views:

71

answers:

3

Is there any point in Unit-Testing a method that the only thing it does is delegate work on another object? Example:

class abc {

    ...

    public void MoveLeft()
    {
        fallingPiece.MoveLeft();
    }

    ...
}

I am doing Unit-Tests for some existing classes I have, for learning purposes. It seems kinda odd to do a Unit-Test for this MoveLeft() method, for example. But I am unsure how would it have been had I done Test-First.

Thanks

+1  A: 

My understanding of unit tests is that they are there to ensure that the logic inside a method stays the same when you didn't intend for it to change, and this method has no logic in it. We have a lot of pass-through methods like that in the code base where I work. Ostensibly, they're "Controller" classes, but in most cases all they do is pass through to the data layer.

Yes, you can unit test them, assuming you have a way to mock fallingPiece. If you actually plan on expanding the MoveLeft method to include logic, it's probably a good idea.

However, to my comment above, it's probably a better move to just inline the method until you actually need to introduce logic around moving left.

arootbeer
Hmm, I didn't get what you meant by inlining the method. Could you be a bit more clear? Thanks
devoured elysium
by inlining I think he means wherever you have MoveLeft() put fallingPiece.MoveLeft(). This might not be such agood idea though if you have abc.MoveLeft and you replace it with abc.fallingPiece.MoveLeft().
derdo
I do agree that abc.fallingPiece.MoveLeft() would be bad form. I'd have to see more of the code to make a constructive comment at this point. However, seeing your response below regarding this being a non-public method, no one would be calling abc.fallingPiece.MoveLeft() anyway; just some other code inside abc would be calling fallingPiece.MoveLeft().
arootbeer
+3  A: 

Will your code break if I do this ? If it would, then you need a test to catch it.

class abc {
    ...
    public void MoveLeft()
    {
        // fallingPiece.MoveLeft();
    }
    ...
}

Assumptions: abc is a public / exposed type and fallingPiece is a dependency. If this holds, then you need a test to test the MoveLeft behavior. If it isn't a public type, then you need a test for the public type XYZ that uses abc as a colloborator/dependency. You don't directly test it but it still needs to be tested.

Gishu
Hmmm. I hadn't thought about abc's acessibility. I'd say it won't probably be public to the outside world. FallingPiece is indeed a dependency.
devoured elysium
@devoured ... see update.
Gishu
A: 

One case that this method can fail is when fallingPiece is null when Abc.MoveLeft gets called. Having a test case that builds a legitimate abc and calls abc.MoveLeft might be good idea. something like CanMoveLeft() { Abc abc =new Abc(); abc.MoveLeft(); Assert.That( abc.fallingPice has moved to left) }

derdo