Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct proxy type in InterfaceProxy #944

Merged
merged 3 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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