Skip to content

Commit

Permalink
SetupAllProperties doesn't setup properties that don't have setter
Browse files Browse the repository at this point in the history
SetupAllProperties doesn't setup properties that only have a get. This
is a regression from previous behavior and this change fixes the issue.
  • Loading branch information
NGloreous committed Oct 19, 2014
1 parent 9645621 commit 97a0f14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
27 changes: 22 additions & 5 deletions Source/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,15 @@ internal static void SetupAllProperties(Mock mock)
var properties = mockType.GetProperties()
.Concat(mockType.GetInterfaces().SelectMany(i => i.GetProperties()))
.Where(p =>
p.CanRead && p.CanWrite &&
p.CanRead && p.CanOverrideGet() &&
p.GetIndexParameters().Length == 0 &&
p.CanOverrideGet() && p.CanOverrideSet())
!(p.CanWrite ^ (p.CanWrite & p.CanOverrideSet())))
.Distinct();
var method = mock.GetType().GetMethods()
var setupPropertyMethod = mock.GetType().GetMethods()
.First(m => m.Name == "SetupProperty" && m.GetParameters().Length == 2);
var setupGetMethod = mock.GetType().GetMethods()
.First(m => m.Name == "SetupGet" && m.GetParameters().Length == 1);
foreach (var property in properties)
{
Expand All @@ -684,8 +686,23 @@ internal static void SetupAllProperties(Mock mock)
SetupAllProperties(mocked.Mock);
}
method.MakeGenericMethod(property.PropertyType)
.Invoke(mock, new[] { expression, initialValue });
if (property.CanWrite)
{
setupPropertyMethod.MakeGenericMethod(property.PropertyType)
.Invoke(mock, new[] { expression, initialValue });
}
else
{
var genericSetupGetMethod = setupGetMethod.MakeGenericMethod(property.PropertyType);
var returnsMethod =
genericSetupGetMethod
.ReturnType
.GetInterface("IReturnsGetter`2", ignoreCase: false)
.GetMethod("Returns", new Type[] { property.PropertyType });
var returnsGetter = genericSetupGetMethod.Invoke(mock, new[] { expression });
returnsMethod.Invoke(returnsGetter, new[] { initialValue });
}
}
});
}
Expand Down
9 changes: 8 additions & 1 deletion UnitTests/StubExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ public void ShouldStubPropertyWithInitialValue()
[Fact]
public void StubsAllProperties()
{
var mock = new Mock<IFoo>();
var mock = new Mock<IFoo>(MockBehavior.Strict);

mock.SetupAllProperties();

// Verify defaults
Assert.Equal(default(int), mock.Object.ValueProperty);
Assert.Equal(null, mock.Object.Object);
Assert.Equal(null, mock.Object.Bar);
Assert.Equal(null, mock.Object.GetOnly);

mock.Object.ValueProperty = 5;
Assert.Equal(5, mock.Object.ValueProperty);

Expand Down Expand Up @@ -98,6 +104,7 @@ public interface IFoo
int ValueProperty { get; set; }
object Object { get; set; }
IBar Bar { get; set; }
object GetOnly { get; }
}

public class Derived : Base
Expand Down

0 comments on commit 97a0f14

Please sign in to comment.