Setup
- Solution uses prism
- Modules Project which is written in VB
- Test Project which is written in C#
In my unit tests I mock up services which are used by modules and pass them, however when module tries to resolve, it fails with the following exception
{"Resolution of the dependency failed, type = "UnderwritingWorkflow.Common.Services.IValidationService", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, UnderwritingWorkflow.Common.Services.IValidationService, is an interface and cannot be constructed. Are you missing a type mapping?
At the time of the exception, the container was:
Resolving UnderwritingWorkflow.Common.Services.IValidationService,(none)
“}
Here is the code for registering object with UnityContainer:
var validationService = new Mock<IValidationService>(MockBehavior.Strict);
validationService.Setup(x => x.Validate(Moq.It.IsAny<object>()))
.Returns(() => new ValidationResults());
Container.RegisterInstance(validationService.Object);
Also if try resolving IServiceValidation from c# straight after registering it (from within the constructor) seems to work:
Container.Resolve<IValidationService>();
Any ideas why this is happening?
Note: I am using Moq mocking framework which creates classes in runtime and I wonder if it is the limitation of VB which doesn’t allow me to use it?
I found what was causing this issues, it is the limitation of the Moq framework. Aparently Moq can’t mock static methods, including extension methods and Container.Resolve is an extension method, hence it fails.