1

I have a class like this:

public class MyClass
{
    public async Task MethodA()
    {
        await DoSomething();
    }

    public Task MethodB()
    {
        return MethodA();
    }
}

I need to test that MethodB calls MethodA.

But how could I verify this?

I'm trying this:

var myClassMock = new Mock<MyClass>();
myClassMock.VerifyAll();
await myClassMock.Object.MethodB();

myClassMock.Verify(d => d.MethodA(), Times.Once);

And getting NotSupportedException: Invalid verify on a non-virtual (overridable in VB) member: d => d.MethodA().

Can I actually test it without using another mocking framework?

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
InfernumDeus
  • 1,185
  • 1
  • 11
  • 33

2 Answers2

0

This would not work anyway, because you call MethodB() on your mock not the real thing. You want to mock MethodA() but use the real implementation of MethodB(). This is called a partial mock. See Using moq to mock only some methods for how to do that.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
  • 1
    Linked question describes different situation. Anyway I decided that it would be easier to just make MethodA virtual. – InfernumDeus Aug 16 '18 at 08:27
0

You can only mock (or verify) a method if you are mocking an interface, or if it's a concrete class and the method is virtual.

Does DoSomething() rely on any dependencies? If not, you could just unit test your class without Moq. If it does, you could mock those dependencies and verify there.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64