mardi 3 avril 2018

System.InvalidOperationException when trying to mock a dependency and register with an IoC container

We have an interface...

public interface IService<T> where T : class
{
}

...which I need to mock as part of a component test project and register with a Castle Windsor container.

All I care about is that this interface has an implementation registered with the container to satisfy dependencies in the project.

I've found there's no way to generically mock such an interface (Mock<IService<>>? Mock<T, IService<T>>?) so I created a dummy class...

public class FakeT
{
}

...and registered the servcie like this...

container.Register(Component.For(typeof(IService<>), typeof(Mock<IService<FakeT>>)).LifestyleTransient());

...which at least compiles, but when I run a test, at the point Castle Windsor tries to resolve this dependency it throws the following exception:

[My.Namespace.IService`1[My.Tests.FakeT]] is not a GenericTypeDefinition. MakeGenericType may only be called on a type for which Type.IsGenericTypeDefinition is true.'

I'm obviously getting this totally wrong, so hopefully you can help :)

Aucun commentaire:

Enregistrer un commentaire