Maurice Klimek
Maurice Klimek

Reputation: 1092

How to test a method that calls another class' method?

I'm fairly new to unit testing. I've recently encountered a problem where I test a method that does one thing only - it calls a method of an object that's part of the class. The class that this object represents has it's own unit tests.

I understand that this method may change in time, and when it does the test should inform me about if the expected result it. But what can I test in such a method?

My code:

public class MyClassToBeTested
{
    private CustomType myObject;

    private const myParameter = 2;
    (...)
    public string MyProperty
    {
        get
        {
            return myObject.DoYourStuff(myParameter);
        }
    }
}

Upvotes: 1

Views: 1254

Answers (2)

Miroslav Trninic
Miroslav Trninic

Reputation: 3455

If something is dependent on something else. i.e. method calls another method, then you should mock it, or simulate its behaviour.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272257

This sounds like you need to capture the call to the underlying object and inspect it (or at least verify that the call has been made). I would mock this object and inject a reference to it (Inversion of Control).

By injecting the object you can provide the real object at deploy time, and the mock during testing.

Upvotes: 1

Related Questions