Skip to content

Commit

Permalink
Merge pull request #944 from stakx/interfaceproxy
Browse files Browse the repository at this point in the history
Use correct proxy type in `InterfaceProxy`
  • Loading branch information
stakx authored Oct 17, 2019
2 parents 247cb31 + facbca4 commit cfb10de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Fixed

* AmbiguousMatchException when setting up the property, that hides another one (@ishatalkin, #939)
* `ArgumentException` ("Interface not found") when setting up `object.ToString` on an interface mock (@vslynko, #942)

## 4.13.0 (2019-08-31)

Expand Down
6 changes: 3 additions & 3 deletions src/Moq/ProxyFactories/InterfaceProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed override bool Equals(object obj)
{
// Forward this call to the interceptor, so that `object.Equals` can be set up.
var interceptor = (IInterceptor)((IProxy)this).Interceptor;
var invocation = new Invocation(interceptor.GetType(), equalsMethod, obj);
var invocation = new Invocation(this.GetType(), equalsMethod, obj);
interceptor.Intercept(invocation);
return (bool)invocation.ReturnValue;
}
Expand All @@ -38,7 +38,7 @@ public sealed override int GetHashCode()
{
// Forward this call to the interceptor, so that `object.GetHashCode` can be set up.
var interceptor = (IInterceptor)((IProxy)this).Interceptor;
var invocation = new Invocation(interceptor.GetType(), getHashCodeMethod);
var invocation = new Invocation(this.GetType(), getHashCodeMethod);
interceptor.Intercept(invocation);
return (int)invocation.ReturnValue;
}
Expand All @@ -49,7 +49,7 @@ public sealed override string ToString()
{
// Forward this call to the interceptor, so that `object.ToString` can be set up.
var interceptor = (IInterceptor)((IProxy)this).Interceptor;
var invocation = new Invocation(interceptor.GetType(), toStringMethod);
var invocation = new Invocation(this.GetType(), toStringMethod);
interceptor.Intercept(invocation);
return (string)invocation.ReturnValue;
}
Expand Down
51 changes: 51 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3150,6 +3150,57 @@ private void SetupIntMethod(Action callback)

#endregion

#region 942

public class Issue942
{
public interface IContent
{
string Name { get; set; }
}

public interface IContainer
{
IContent Content { get; set; }
}

private const string toStringReturnValue = "some string here";

[Fact]
public void Setup_ToString_before_Name()
{
this.TestImpl(contentMock =>
{
contentMock.Setup(c => c.ToString()).Returns(toStringReturnValue);
contentMock.Setup(c => c.Name);
});
}

[Fact]
public void Setup_ToString_after_Name()
{
this.TestImpl(contentMock =>
{
contentMock.Setup(c => c.Name);
contentMock.Setup(c => c.ToString()).Returns(toStringReturnValue);
});
}

private void TestImpl(Action<Mock<IContent>> setup)
{
var contentMock = new Mock<IContent>();
var containerMock = new Mock<IContainer>();

containerMock.Setup(c => c.Content).Returns(contentMock.Object);
setup(contentMock);

Assert.Equal(toStringReturnValue, contentMock.Object.ToString());
Assert.Equal(toStringReturnValue, containerMock.Object.Content.ToString());
}
}

#endregion

// Old @ Google Code

#region #47
Expand Down

0 comments on commit cfb10de

Please sign in to comment.