I have this interface:
public interface IViewModelCache
{
IViewModel GetViewModel(Type viewModelType);
}
And I'm mocking it with this code:
var cacheMock = Mock.Of<IViewModelCache>();
Mock.Get(cacheMock)
.Setup(cache => cacheMock.GetViewModel(typeof(IViewModel)))
.Returns(Mock.Of<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](Func
1 function) at Moq.Mock.Setup[T,TResult](Mock1 mock, Expression
1 expression, Condition condition) at Moq.Mock1.Setup[TResult](Expression
1 expression)
I have also tried that:
Mock.Get(cacheMock)
.Setup(cache => cacheMock.GetViewModel(It.IsAny<Type>()))
.Returns(Mock.Of<IViewModel>());
And that one:
Mock.Get(cacheMock)
.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);
Mock.Get(cache)
.Setup(mock => mock.GetViewModel(viewModelType))
.Returns(viewModel);