ninject / Ninject.MockingKernel

Extension for Ninject aiding testability in Moq, NSubstitute, and FakeItEasy
http://ninject.org/
Other
62 stars 25 forks source link

MoqKernel.GetMock<T> throws ArgumentException when T is Func<...> #39

Open telamar opened 3 years ago

telamar commented 3 years ago

There is the following example: 1) Interface public interface ICustomInterface { ... }

2) Class public class CustomObject { public CustomObject(Func< ICustomInterface > factoryFunc) { ... } }

3) Test [SetUp] public void TestInitialize() { Mock<Func< ICustomInterface >> factoryFuncMock = MoqMockingKernel.GetMock<Func< ICustomInterface >>(); <-- ArgumentException factoryFuncMock.Setup(...).Returns(...); }

As I understand, the exception is thrown because Mock.Get< T >(T) waits for Func< ICustomInterface >, but MoqMockingKernel.Get<Func< ICustomInterface >> returns a FunctionFactory<> type instead of Func<>

This case is helpful when we try to inject Func< ... > with several generic types and then we want to check that Func was called with correct arguments Example: Mock<Func<bool, ICustomInterface>> factoryFuncMock = MoqMockingKernel.GetMock<Func<bool, ICustomInterface>>(); factoryFuncMock.Setup(factoryFunc => factoryFunc(true)).Returns(...);

scott-xu commented 3 years ago

Try explicitly bind your Func<ICustimInterface> to Mock