Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial mocking of class with Moq

Tags:

c#

.net

mocking

moq

I want to mock only the GetValue method of the following class, using Moq:

public class MyClass
{
    public virtual void MyMethod()
    {
        int value = GetValue();
        Console.WriteLine("ORIGINAL MyMethod: " + value);
    }

    internal virtual int GetValue()
    {
        Console.WriteLine("ORIGINAL GetValue");
        return 10;
    }
}

I already read a bit how this should work with Moq. The solution that I found online is to use the CallBase property, but that doesn't work for me.

This is my test:

[Test]
public void TestMyClass()
{
     var my = new Mock<MyClass> { CallBase = true };
     my.Setup(mock => mock.GetValue()).Callback(() => Console.WriteLine("MOCKED GetValue")).Returns(999);
     my.Object.MyMethod();
     my.VerifyAll();
 }

I would expect that Moq uses the existing implementation of MyMethod and calls the mocked method, resulting in the following output:

ORIGINAL MyMethod: 999
MOCKED GetValue

but that's what I get :

ORIGINAL GetValue
ORIGINAL MyMethod: 10

and then

Moq.MockVerificationException : The following setups were not matched: MyClass mock => mock.GetValue()

I got the feeling, that I misunderstood something completely. What am I missing here? Any help would be appreciated

like image 929
Fabian Avatar asked Jun 13 '12 15:06

Fabian


People also ask

Can you mock a class 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. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.

What can be mocked with Moq?

Unit testing is a powerful way to ensure that your code works as intended. It's a great way to combat the common “works on my machine” problem. Using Moq, you can mock out dependencies and make sure that you are testing the code in isolation. Moq is a mock object framework for .

How do you use partial mock?

Partial mocks allow you to mock some of the methods of a class while keeping the rest intact. Thus, you keep your original object, not a mock object, and you are still able to write your test methods in isolation. Partial mocking can be performed on both static and instance calls. This is an elevated feature.

How does Moq mock work?

Mock objects allow you to mimic the behavior of classes and interfaces, letting the code in the test interact with them as if they were real. This isolates the code you're testing, ensuring that it works on its own and that no other code will make the tests fail.


1 Answers

OK, I found the answer to this in another question: How to Mock the Internal Method of a class?. So this is a duplicate and can be closed.

Nevertheless, here's the solution: just add this line to the Assembly.config of the project you want to test:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] // namespace in Moq
like image 89
Fabian Avatar answered Oct 03 '22 18:10

Fabian