Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 853
michael Avatar asked Oct 15 '25 09:10

michael


1 Answers

Do you have a preference for mocking frameworks?

The Partial Mock feature in Rhino Mocks seems like it should do what you want.

like image 103
Nader Shirazie Avatar answered Oct 16 '25 22:10

Nader Shirazie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!