-1

So I am trying to test a method call on another interface of my mock. Tried a few options and some tweaking here and there but it never registers the method as actually called. When I debug through the code/test the method is being called. The actual code is quite complex, so I created this abstract example of the problem. The class and method I want to test looks like the following:

public class ClassToTest
{
    private readonly _domainRunner;

    public ClassToTest(IDomainRunner domainRunner)
    {
        _domainRunner = domainRunner;
    }

    public void MethodToTest()
    {
        (_domainRunner as IRunner).RunAnotherTask();
    }
}

The IDomainRunner and its relevant method I am trying to verify looks like this:

public class DomainRunner : IRunner, IDomainRunner 
{

    ...

    void IRunner.RunAnotherTask()
    {
        // irrelevant
    }
}

And just for the complettness the basic test structure:

[TestClass]
public class DomainRunnerTests 
{
    [TestMethod]
    public void TestRunSomeTask()
    {
        var domainRunnerMock = new Mock<IDomainRunner>();

        var someClass = new ClassToTest(domainRunnerMock.Object):

        // How to Assert that IRunner.RunAnotherTask was called?
    }
} 

I've tried to setup the Method RunAnotherTask or to directly verify it, neither was successful. This code does not really seem untestable to me or i am that wrong?

mamichels
  • 619
  • 4
  • 12
  • 2
    Does this answer your question? [How to mock a class that implements multiple interfaces](https://stackoverflow.com/questions/15820642/how-to-mock-a-class-that-implements-multiple-interfaces) – A Friend Mar 13 '20 at 11:07
  • How did this not came up on google. Thank you, it indicated a good approach. I'll incorporate it into the question. – mamichels Mar 13 '20 at 11:09

1 Answers1

0

As refered by @A Friend the solution is as follows:

[TestClass]
public class DomainRunnerTests 
{
    [TestMethod]
    public void TestRunSomeTask()
    {
        var domainRunnerMock = new Mock<IDomainRunner>();
        var runnerMock = domainRunnerMock.As<IRunner>:
        runnerMock.Setup(m => m.RunAnotherTask()).Verifiable();

        var someClass = new ClassToTest(domainRunnerMock.Object):

        runnerMock.Verify();
    }
}
mamichels
  • 619
  • 4
  • 12