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
publicinterfaceIRepository{IList<T>GetEntityFieldsById<T>(Guidid,Expression<Func<EntityClass,T>>selector);}
...var result =_repository.GetEntityFieldsById(id, x =>new{x.StartDate,x.EndDate});
...
Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: Moq.MockException: IRepository.GetEntityFieldsById<<>f__AnonymousType02>(d1b3c6fb-5a5e-4f0a-9f68-191ca6a61bff,
x => new <>f__AnonymousType02(StartDate = x.StartDate, EndDate = x.EndDate)) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.
The text was updated successfully, but these errors were encountered:
When Moq looks for a setup matching the invocation, the invoked method's signature is compared with that of the set up method's (see this method). Return types must match exactly (see this line of code). So even though anonymous types are assignment-compatible with object, they are still a different type from object, so setups don't get matched.
I am not sure whether this can or should be fixed (probably not), but #343 might help.
Making this scenario work by relaxing Moq's method matching logic would be wrong. #343 is likely to be the solution for this problem, if one exists at all.
Repro 1:
Quoting from the Stack Overflow question, "Moq a function with anonymous type" (see there for details):
Repro 2:
Quoting from another Stack Overflow question, "Setup method with signature
IList<T> Get<T>(Guid id, Expression<Func<EntityName, T>> selector)
" (see there for details):The text was updated successfully, but these errors were encountered: