You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But I don't know how to setup for dynamic return types. (afraid its not possible)
So my real call looks like this where I return just an anonymous class and not a DTO in the selection func:
And when I make the setup with It.IsAny<Expression<Func<Account, It.IsAnyType>>>() I don't know how to write the return correctly because it forces return type TResult to be It.IsAnyType which is not possible.
Remember that dynamic is essentially a feature of the .NET compilers, but to the runtime, it is the same thing as System.Object. Since Moq version 4 does all of its work exclusively at runtime, it only sees System.Object wherever your source code mentions dynamic.
Generally speaking, Moq doesn't really have any support for dynamic, because one of its explicit design goals was static type safety, and dynamic and all of its late binding trickery directly goes against that. So I can't really offer you a good solution.
What you could try (but it's probably not going to be very pretty) is to look into It.IsAnyType placeholders – try using It.IsAnyType in place of dynamic inside your setup expression, and give your ReturnsAsync lambda a single (IInvocation invocation) parameter. In the lambda's body, you can then inspect the actual runtime Expression<> type via invocation.Method.GetParameters().First().ParameterType and do a bunch of reflection to produce a value of the appropriate type.
I have an interface that looks like this:
I can pass an selector func to get exactly the data structure that I need.
e.g.:
For this I can write the setup like:
But I don't know how to setup for dynamic return types. (afraid its not possible)
So my real call looks like this where I return just an anonymous class and not a DTO in the selection func:
Now I can not simply change the setup type to dynamic/object because then the return method is never called:
And when I make the setup with
It.IsAny<Expression<Func<Account, It.IsAnyType>>>()
I don't know how to write the return correctly because it forces return typeTResult
to beIt.IsAnyType
which is not possible.Full unit test to play around: https://gist.github.com/matthiaslischka/f1aa29e285c75af13e5851d71b080ce8
Is what I want even possible?!
Do you see a better way? I don't want to use DTOs in my production code just for test setup purpose...
BR Matthias
The text was updated successfully, but these errors were encountered: