1

I am using Moq v4.13 and C#. I am trying to test method MethodA on the interface of MyClass : IMyInterface. MethodA has invocations on methods of another interface (IAnother) and I have set up those. These seem to be setup fine.

MethodA also invokes MethodB (also on IMyInterface) and MethodC (public, but not on IMyInterface) in MyClass.

I have set up MethodB but while testing, it seems like actual code of MethodB is invoked.

As for MethodC, I am not sure how to set that up.

public interface MyInterface 
{
    int MethodA();
    int MethodB();
}
public class MyClass : MyInterface 
{
    public MyClass(IAnother b) {...}
    
    public int MethodB(){...}
    public int MethodC(){...}

    public int MethodA()
    {
        ...
        var x = b.MethodX();
        ...
        var a = MethodB();
        ...
        var b = MethodC()

        return a + b + x;
    }
}
public MyInterfaceHarness
{
   Mock<IAnother> _anotherMock;

   public MyInterfaceHarness() 
   {
       _anotherMock = new Mock<IAnother>();
       Mocker = new AutoMocker();
   }

   public AutoMocker Mocker {get;}
   
   public MyClass MethodAHarness()
   {
       Mocker.Use(_anotherMock)  // Mocker is Automocker
       _anotherMock.Setup(m => m.MethodX()).Returns(5); // this seems to be setup fine

      //here I want to setup invocations to MethodB and MethodC
      ....

      Mocker.CreateInstance<MyClass>();
   }
}
[TestFixture]
public MyInterfaceTest 
{
    private  MyInterfaceHarness _harness;
    private AutoMocker _mocker;

    [SetUp]
    public void SetUp()
    {
       _harness = new MyInterfaceHarness();
       _mocker = _harness.Mocker();
    }

    [Test]
    public void Should_Test_MethodA()
    {
        var mi = _harness.MethodAHarness();

        // use _mocker to setup other invocations that need verify            
        ...

        var i = mi.MethodA();
       
       //asserts
       ...
        
    }
}
Anand
  • 1,387
  • 2
  • 26
  • 48
  • @Anand Please try to provide valid C# code. For example in C# you can't specify the visibility of an interface method. – Peter Csala Jan 04 '21 at 07:35
  • @Anand By the way why do you want to mock out `MethodB` and `MethodC`? Your **System Under Test** is your `MyClass`, not your `MethodA`. You should mock only dependencies, not some parts of the SUT itself. – Peter Csala Jan 04 '21 at 07:38
  • @PeterCsala I have fixed the methods defined in the interface. I am new to mocking so I thought without setup of MethodB and MethodC, I would not get a meaningful value to assert. – Anand Jan 04 '21 at 13:27
  • @Anand As I said you don't have to mock `MethodB` and `MethodC`. They are part of your Unit so they are not dependencies. You can and should mock `MethodX`. Even though your assertion might look a bit silly it should not bother you. Your simplified example could be verified that easily. – Peter Csala Jan 04 '21 at 13:51
  • Do you have any further question? – Peter Csala Jan 05 '21 at 09:05
  • No further question, thanks so much @PeterCsala – Anand Jan 05 '21 at 19:44

0 Answers0