C# Moq Mocking a method that expects a Func<TResult> delegate

28 Views Asked by At

I need to setup a method with Moq so that I can control what it returns. The setup I'm trying to do looks like this:

Setup(x => x.DoSomething<TResult>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<Func<TResult>>()).Returns(10)

However I'm getting the following error:

The type or namespace name 'TResult' could not be found (are you missing a using directive or an assembly reference?).

The original method that I'm trying to setup, which I did not create, recognizes TResult but it's not defined or referenced anywhere as far as I can tell.

This says it's part of the system namespace which I'm referencing in my test class so I'm confused why the test class doesn't seem to know what TResult is: https://learn.microsoft.com/en-us/dotnet/api/system.func-1?view=net-7.0

I've tried to use: It.IsAnyType' 'Is.IsAny<Delegate>()' and variations of 'It.IsAny<Action<string>>()

but nothing seems to satisfy the compiler.

edit the original method signature is the signature is: public TResult DoSomething<TResult>( string, int, Func<TResult> DoSomethingElse)

Edit someone suggested this https://andersmalmgren.com/2021/02/28/mock-and-callback-a-generic-method-with-unavailable-type-using-moq/ and then closed my question. I don't see how that suggestion is relevant to my issue. I don't have as specific event to mock like "onEvent".

0

There are 0 best solutions below