How to simulate the interface that accepts the Type argument and returns another interface


I have this interface:

public interface IViewModelCache
    IViewModel GetViewModel(Type viewModelType);

And I'm mocking it with this code:

var cacheMock = Mock.Of<IViewModelCache>();

    .Setup(cache => cacheMock.GetViewModel(typeof(IViewModel)))

But it throws

"Specified method is not supported.",

what is wrong?

The stack trace is:

at Moq.Mock.FluentMockVisitor.VisitMember(MemberExpression node)
at Moq.Mock.FluentMockVisitor.Accept() at Moq.Mock.FluentMockVisitor.Accept(Expression expression, Mock mock)
at Moq.Mock.GetInterceptor(Expression fluentExpression, Mock mock)
at Moq.Mock.<>c__DisplayClass65_02.<Setup>b__0() at Moq.PexProtector.Invoke[T](Func1 function) at Moq.Mock.Setup[T,TResult](Mock1 mock, Expression1 expression, Condition condition) at Moq.Mock1.Setup[TResult](Expression1 expression)

I have also tried that:

   .Setup(cache => cacheMock.GetViewModel(It.IsAny<Type>()))

And that one:

   .Setup(cache => cacheMock.GetViewModel(It.IsAny<Type>()))
   .Returns(new Mock<IViewModel>().Object);

Both throws the same exception.

The expression in the Setup is wrong. Try making the code cleaner so that the intent is understood.

var cache = Mock.Of<IViewModelCache>();
var viewModel = Mock.Of<IViewModel>();
var viewModelType = typeof(IViewModel);

   .Setup(mock => mock.GetViewModel(viewModelType))