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

Let .As<T> mocks generate same proxy as the uncast mock #460

Merged
merged 3 commits into from
Sep 28, 2017
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1

* Update package reference to `Castle.Core` (DynamicProxy) from version 4.1.1 to 4.2.0, which uses a new assembly versioning scheme that should eventually reduce assembly version conflicts and the need for assembly binding redirects (@stakx, #459)

#### Fixed

* `mock.Object` should always return the exact same proxy object, regardless of whether the mock has been cast to an interface via `.As<T>()` or not (@stakx, #460)


## 4.7.127 (2017-09-26)

Expand Down
5 changes: 5 additions & 0 deletions Source/AsInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,10 @@ public override Mock<TNewInterface> As<TNewInterface>()
{
return this.owner.As<TNewInterface>();
}

protected override object OnGetObject()
{
return this.owner.Object;
}
}
}
2 changes: 2 additions & 0 deletions UnitTests/AsInterfaceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public void ShouldNotThrowIfCallExplicitlyImplementedInterfacesMethodWhenCallBas
bag.Get("test");
}

// see also test fixture `Issue458` in `IssueReportsFixture`

public interface IFoo
{
void Execute();
Expand Down
22 changes: 22 additions & 0 deletions UnitTests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,28 @@ public class Foo

#endregion

#region 458

public class Issue458
{
[Fact]
public void Mock_Object_always_returns_same_object_even_when_first_instantiated_through_AsInterface_cast()
{
Mock<IFoo> mock = new Mock<Foo>().As<IFoo>();

object o1 = ((Mock)mock).Object;
object o2 = mock.Object;

Assert.Same(o1, o2);
}

public interface IFoo { }

public class Foo : IFoo { }
}

#endregion

// Old @ Google Code

#region #47
Expand Down