How to call constructor with interface arguments when mocking a concrete class with Moq
Asked Answered
C

3

17

I have the following class, which uses constructor injection:

public class Service : IService
{
    public Service(IRepository repository, IProvider provider) { ... }
}

For most methods in this class, I simply create Moq mocks for IRepository and IProvider and construct the Service. However, there is one method in the class that calls several other methods in the same class. For testing this method, instead of testing all those methods together, I want to test that the method calls those methods correctly and processes their return values correctly.

The best way to do this is to mock Service. I've mocked concrete classes with Moq before without issue. I've even mocked concrete classes that require constructor arguments with Moq without issue. However, this is the first time I've needed to pass mocked arguments into the constructor for a mocked object. Naturally, I tried to do it this way:

var repository = new Mock<IRepository>();
var provider = new Mock<IProvider>();

var service = new Mock<Service>(repository.Object, provider.Object);

However, that does not work. Instead, I get the following error:

Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: My.Namespace.Service.
Could not find a constructor that would match given arguments:
    Castle.Proxies.IRepository
    Castle.Proxies.IProvider

This works fine if Service's constructor takes simple arguments like ints and strings, but not if it takes interfaces that I'm mocking. How do you do this?

Calley answered 18/4, 2014 at 14:47 Comment(1)
Which Moq and castle version are you using? Because your sample code is working fine using Moq 4.2.1312.1622 and Castle.Core 3.2.2 ...Ironworks
I
14

The issue is that you are mocking the service that you are trying to test.

If you are wanting to test the implementation of the Service class (whether that be calls to mocked objects or not), all you need are mocks for the two interfaces, not the test class.

Instead of:

var repository = new Mock<IRepository>();
var provider = new Mock<IProvider>();

var service = new Mock<Service>(repository.Object, provider.Object);

It should be this instead.

var repository = new Mock<IRepository>();
var provider = new Mock<IProvider>();

var service = new Service(repository.Object, provider.Object);

Mocking concrete objects in general should be a last resort, but it should never be done to the actual target of your test, as that has the side effect of overriding and adding code to the class that you are trying to prove is working correctly--potentially invalidating the goal of the test completely.

I suggest that you do not mock the concrete object, and instead instantiate it normally with mocked dependencies.

Input answered 5/10, 2015 at 19:39 Comment(0)
A
5

I had a very similar problem when my equivalent of Service had an internal constructor, so it was not visible to Moq.

I added

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

to my AssemblyInfo.cs file for the implementing project. Not sure if it is relevant, but I wanted to add a suggestion on the off chance that it helps you or someone else.

Acaulescent answered 4/11, 2014 at 15:39 Comment(0)
E
1

It must be old version issue, all is ok with latest version. Nick, Please check!

P.s.: I started bounty by misstake (I had wrong signature in my constructor).

Emigrant answered 4/10, 2015 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.