2

In the below given scenario, I have added reference to someExternal.Library.dll and derived a class named MyClass from BaseClass. MyClass invokes MakeCall() method of BaseClass internally in its methods. Here, the BaseClass is a concrete class which does not implement any Interface and also the MakeCall() method is a non-virtual method.

in such a scenario, how do I mock only the MakeCall() method of base class? I want to write unit tests for MyClass

public class MyClass : BaseClass
{

public void DoSomething()
{
    //someExternal.Library.dll is referenced to the project
    // Makecall() is available to this class through inheritance 

    MakeCall();
    ...

}

    public bool DoSomethingElse()
{
        ...
    }
}

in the above snippet I want to write unit Test for MyClass and I should be able to Mock obj.MakeCall(); method call using MoQ.

NOTE: MOQ is the only mocking framework I am allowed to use

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
Nidhin
  • 33
  • 1
  • 4

1 Answers1

1

Since your base method neither implements any interface nor marked as virtual, Moq can't help you at that specific scenario, since it generates a proxy that will implement the interface or create a derived class that overrides a method in order to intercept calls.

If your base class is not a legacy code you can't touch by any means, I would recommend to add an interface and mock it directly.

Otherwise, see the following answer for Moq alternatives that work directly with the IL and can mock sealed classes:

https://stackoverflow.com/a/21793891/3400897

Max Mor
  • 96
  • 1
  • 6