Say I have the following:
public interface ISession
{
T Get(dynamic filter); }
}
And I have the following code that I want to
You can use the It.Is
matcher together with reflection. You cannot use dynamic in expression trees so It.Is
won't work that's why you need reflection to get the your property value by name:
sessionMock
.Setup(x => x.Get(
It.Is
Where GetPropertyValue
is a little helper:
public static class ReflectionExtensions
{
public static T GetPropertyValue(this object obj, string propertyName)
{
return (T) obj.GetType().GetProperty(propertyName).GetValue(obj, null);
}
}