C#: How to unit test a method that relies on another method within the same class?

Posted by michael paul on Stack Overflow See other posts from Stack Overflow or by michael paul
Published on 2011-01-07T22:20:58Z Indexed on 2011/01/07 22:53 UTC
Read the original article Hit count: 157

I have a class similar to the following:

public class MyProxy : ClientBase<IService>, IService
{
    public MyProxy(String endpointConfiguration) :
        base(endpointConfiguration) { }

    public int DoSomething(int x)
    {
        int result = DoSomethingToX(x); //This passes unit testing

        int result2 = ((IService)this).DoWork(x)

        //do I have to extract this part into a separate method just
        //to test it even though it's only a couple of lines?
        //Do something on result2
        int result3 = result2 ...

        return result3;
    }

    int IService.DoWork(int x)
    {
        return base.Channel.DoWork(x);
    }
}

The problem lies in the fact that when testing I don't know how to mock the result2 item without extracting the part that gets result3 using result2 into a separate method. And, because it is unit testing I don't want to go that deep as to test what result2 comes back as... I'd rather mock the data somehow... like, be able to call the function and replace just that one call.

© Stack Overflow or respective owner

Related posts about c#

Related posts about unit-testing