Skip to content

Commit

Permalink
Merge pull request #1185 from tonyhallett/fix-protectedasmock
Browse files Browse the repository at this point in the history
Fix virtual properties and automocking for `mock.Protected().As<>()`
  • Loading branch information
stakx committed Jul 19, 2021
2 parents a6fde8b + c8cfce2 commit 1aaa220
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,8 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1

#### Fixed

* Virtual properties and automocking not working for `mock.Protected().As<>()` (@tonyhallett, #1185)

* Issue mocking VB.NET class with overloaded property/indexer in base class (@myurashchyk, #1153)


Expand Down
6 changes: 3 additions & 3 deletions src/Moq/Protected/ProtectedAsMock.cs
Expand Up @@ -217,7 +217,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
}
else
{
return node;
return base.VisitMethodCall(node);
}
}

Expand All @@ -230,7 +230,7 @@ protected override Expression VisitMember(MemberExpression node)
}
else
{
return node;
return base.VisitMember(node);
}
}

Expand Down Expand Up @@ -280,7 +280,7 @@ private PropertyInfo FindCorrespondingProperty(PropertyInfo duckProperty)
{
var candidateTargetProperties =
this.targetType
.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(ctp => IsCorrespondingProperty(duckProperty, ctp))
.ToArray();

Expand Down
60 changes: 60 additions & 0 deletions tests/Moq.Tests/ProtectedAsMockFixture.cs
Expand Up @@ -130,6 +130,13 @@ public void Setup_can_setup_generic_method()
Assert.Equal(3, handledMessages.Count);
}

[Fact]
public void Setup_can_automock()
{
this.protectedMock.Setup(m => m.Nested.Method(1)).Returns(123);
Assert.Equal(123, mock.Object.GetNested().Method(1));
}

[Fact]
public void SetupGet_can_setup_readonly_property()
{
Expand All @@ -150,6 +157,25 @@ public void SetupGet_can_setup_readwrite_property()
Assert.Equal(42, actual);
}

[Fact]
public void SetUpGet_can_automock()
{
this.protectedMock.SetupGet(m => m.Nested.Value).Returns(42);

var actual = mock.Object.GetNested().Value;

Assert.Equal(42, actual);
}

[Fact] void SetupGet_can_setup_virtual_property()
{
this.protectedMock.SetupGet(m => m.Virtual).Returns(42);

var actual = mock.Object.GetVirtual();

Assert.Equal(42, actual);
}

[Fact]
public void SetupProperty_can_setup_readwrite_property()
{
Expand Down Expand Up @@ -298,6 +324,12 @@ public void VerifyGet_includes_failure_message_in_exception()
Assert.Contains("Was not queried.", exception.Message);
}

public interface INested
{
int Value { get; }
int Method(int value);
}

public abstract class Foo
{
protected Foo()
Expand Down Expand Up @@ -336,6 +368,32 @@ public int GetSomething()
protected abstract void DoSomethingImpl(int arg);

protected abstract int GetSomethingImpl();

protected abstract INested Nested { get; set; }

public INested GetNested()
{
return Nested;
}

private int virtualProperty;
public virtual int Virtual
{
protected get
{
return virtualProperty;
}
set
{
virtualProperty = value;
}

}

public int GetVirtual()
{
return Virtual;
}
}

public interface Fooish
Expand All @@ -347,6 +405,8 @@ public interface Fooish
void DoSomethingImpl(int arg);
int GetSomethingImpl();
void NonExistentMethod();
INested Nested { get; set; }
int Virtual { get; set; }
}

public abstract class MessageHandlerBase
Expand Down

0 comments on commit 1aaa220

Please sign in to comment.