Skip to content

Commit

Permalink
Make SetupProperty work w/ recursive expressions again
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Mar 6, 2022
1 parent f160ad8 commit f7d5cba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
24 changes: 24 additions & 0 deletions src/Moq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,30 @@ internal static SequenceSetup SetupSequence(Mock mock, LambdaExpression expressi
});
}

internal static StubbedPropertySetup SetupProperty(Mock mock, LambdaExpression expression, object initialValue)
{
Guard.NotNull(expression, nameof(expression));

var pi = expression.ToPropertyInfo();

if (!pi.CanRead(out var getter))
{
Guard.CanRead(pi);
}

if (!pi.CanWrite(out var setter))
{
Guard.CanWrite(pi);
}

return Mock.SetupRecursive(mock, expression, (targetMock, _, _) =>
{
var setup = new StubbedPropertySetup(targetMock, expression, getter, setter, initialValue);
targetMock.MutableSetups.Add(setup);
return setup;
});
}

private static TSetup SetupRecursive<TSetup>(Mock mock, LambdaExpression expression, Func<Mock, Expression, MethodExpectation, TSetup> setupLast, bool allowNonOverridableLastProperty = false)
where TSetup : ISetup
{
Expand Down
17 changes: 1 addition & 16 deletions src/Moq/Mock`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,22 +628,7 @@ public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property)
/// </example>
public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property, TProperty initialValue)
{
Guard.NotNull(property, nameof(property));

var pi = property.ToPropertyInfo();

if (!pi.CanRead(out var getter))
{
Guard.CanRead(pi);
}

if (!pi.CanWrite(out var setter))
{
Guard.CanWrite(pi);
}

var setup = new StubbedPropertySetup(this, property, getter, setter, initialValue);
this.MutableSetups.Add(setup);
Mock.SetupProperty(this, property, initialValue);
return this;
}

Expand Down

0 comments on commit f7d5cba

Please sign in to comment.