Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
telamar opened this issue Sep 16, 2020 · 1 comment
Open

Comments

@telamar
Copy link

telamar commented Sep 16, 2020

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
Copy link
Member

Try explicitly bind your Func<ICustimInterface> to Mock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants