internal static bool SetupReturns(Mock mock, LambdaExpression expression, object value) { PropertyInfo propertyToSet = null; if (expression.Body is MemberExpression me && me.Member is PropertyInfo pi && !(pi.CanRead(out var getter) && getter.CanOverride() && ProxyFactory.Instance.IsMethodVisible(getter, out _)) && pi.CanWrite(out _)) { // LINQ to Mocks allows setting non-interceptable properties, which is handy e.g. when initializing DTOs. // Generally, we prefer having a setup for a property's return value, but if that isn't possible (e.g. // with a non-virtual or sealed property), we will have to fall back to simply setting that property // through reflection. // // The above condition detects exactly those cases. propertyToSet = pi; // The property access (which is one that cannot be setup) must be subtracted from the setup expression: expression = Expression.Lambda(me.Expression, expression.Parameters); if (!expression.CanSplit()) { Mocks.SetProperty(mock, propertyToSet, value); return(true); } } var setup = Mock.Setup(mock, expression, condition: null); if (propertyToSet == null) { setup.SetEagerReturnsResponse(value); } else { Mocks.SetProperty(setup.Mock, propertyToSet, value); } return(true); }