Skip to content

Commit

Permalink
Merge pull request #583 from stakx/callbase
Browse files Browse the repository at this point in the history
Ignore CallBase for members of additional interfaces
  • Loading branch information
stakx authored Jan 25, 2018
2 parents 612c6fa + 47f6036 commit 60e9b18
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Fixed

* Wrong parameters count for extension methods in `Callback` and `Returns` (@Caraul, #575)
* `CallBase` regression with members of additional interfaces (@stakx, #583)


## 4.8.1 (2018-01-08)
Expand Down
25 changes: 25 additions & 0 deletions Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,31 @@ public object DoSomething()

#endregion

#region 582

public sealed class Issue582
{
public interface IFoo
{
void Method();
}

public class Bar
{
}

[Fact]
public void CallBase_has_no_effect_for_methods_of_additional_interfaces()
{
var bar = new Mock<Bar>() { CallBase = true };
var foo = bar.As<IFoo>().Object;

foo.Method();
}
}

#endregion

// Old @ Google Code

#region #47
Expand Down
20 changes: 16 additions & 4 deletions Source/Interception/InterceptionAspects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,23 @@ public override InterceptionAction Handle(Invocation invocation, Mock mock)
Debug.Assert(mock.ImplementsInterface(declaringType));

// Case 2: Explicitly implemented interface method of a class proxy.
// Only call base method if it isn't an event accessor.
if (!method.LooksLikeEventAttach() && !method.LooksLikeEventDetach())

if (mock.InheritedInterfaces.Contains(declaringType))
{
invocation.ReturnBase();
return InterceptionAction.Stop;
// Case 2a: Re-implemented interface.
// The base class has its own implementation. Only call base method if it isn't an event accessor.
if (!method.LooksLikeEventAttach() && !method.LooksLikeEventDetach())
{
invocation.ReturnBase();
return InterceptionAction.Stop;
}
}
else
{
Debug.Assert(mock.AdditionalInterfaces.Contains(declaringType));

// Case 2b: Additional interface.
// There is no base method to call, so fall through.
}
}
}
Expand Down

0 comments on commit 60e9b18

Please sign in to comment.