Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Mock for the abstract base class using MOQ framework?

I want to write unit tests for MyClass but its base class is an abstract class.

public class MyClass : AbstractBaseClass
{
}

I want to Mock the AbstractBase class, so that I can skip some of the logic in its constructor when I create the MyClass instance I want to test. Is there anyway I can do this?

//Unit Test
var test = new Mock<IInterface>();
var derivedclass = new DerivedClass(test.Object);

test.Setup(d=>d.MyMethod(It.IsAny<string>()).returns(2);
derivedclass.MyMethod("hello");


// Derived Class
class DerivedClass : AbstractClass{

     //constuctor
    public DerivedClass(IInterface interface){
        _interface = interface;
    }
    public MyMethod(string str){
        return 2;
    }
}

//Abstract Class
public abstract class AbstractClass
{

 // This method gets called when i create the  instance of the derived class in my unit   
  test..

   protected AbstractedClass() : this(new SomeOtherClass()){
       DoSomethingElse();    /// I want to skip this method or mock it.
   }
 }
like image 288
Dexterslab Avatar asked May 23 '11 18:05

Dexterslab


People also ask

How do you mock a method in Moq?

First, we instantiate the FakeDbArticleMock class and indicate which setup we want to use for this test. Then, it is necessary to instantiate the repository we want to test and inject the mock instance into it. Finally, we call the method we are testing and assert the results.

Can abstract class be mocked?

Mocking abstract class using PowerMockmock() method for mocking the abstract classes. Using PowerMock instead of Mockito. mock() is a better approach as it can have control over the private as well as static methods.

What is Moq mocking framework?

Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes. Moq makes the creation of fakes redundant by using dynamically generated types.

What can be mocked with Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces.


1 Answers

By inheriting from the base class you are extending it. It is more about getting your code into a testable state rather than having Moq work for you.

  1. You can either use composition instead of a base class and then use dependency injection (via an interface) that you can mock.
  2. Or if you have to inherit then extract the logic that you don't want to run into another class that you inject again via dependency injection.
  3. Or have that logic that you don't want to run be part of a virtual method that you can mock. (like @Ohad Horesh's answer:)

    public virtual void DoSomethingElse();
    
    mock.Setup(abs => abs.Foo());  //here the mocked method will be called  
                                   // rather than the real one
    

If these options are not viable then you will either have to test that functionality through the derived class or use another mocking framework such as TypeMock Isolator, Moles or JustMock.

like image 74
aqwert Avatar answered Oct 13 '22 13:10

aqwert