0

I'm trying to mock an action that console writes "hi" when it gets invoked but I'm getting the following error:

Unable to cast object of type 'System.Linq.Expressions.InstanceMethodCallExpressionN' to type 'System.Linq.Expressions.InvocationExpression'.

Here is what I got and maybe I am not doing this correctly:

var actionMock = Mock.Of<Action>();
var getActionMock = Mock.Get(actionMock);

getActionMock.Setup(c => c.Invoke()).Callback(() => {
    Console.Write("hi");
});

Any help or suggestion would be great!

Ron
  • 59
  • 9
  • Not sure if related, but Invoke is not virtual, so not sure if you can mock it like that. Edit: https://stackoverflow.com/questions/11738102/how-to-mock-non-virtual-methods – AndrewP Aug 29 '18 at 00:50
  • 4
    Why would you possibly ever need to mock Action? If you need to send/inject an Action to something you are testing, just create one like normal. You wouldn’t try to mock an int or string. – Dave M Aug 29 '18 at 01:06
  • Thanks that makes sense @AndrewP – Ron Aug 29 '18 at 16:45
  • I initially wanted to mock the action so I can verify that if it got invoked @DaveM – Ron Aug 29 '18 at 16:54
  • You don’t need to mock an action to achieve that. Just make one. – Dave M Aug 29 '18 at 19:38
  • Sounds good and thanks @Dav – Ron Aug 29 '18 at 20:02

0 Answers0