Skip to content

Commit

Permalink
Merge pull request #460 from stakx/asinterface-instantiation-bug
Browse files Browse the repository at this point in the history
Let `.As<T>` mocks generate same proxy as the uncast mock
  • Loading branch information
stakx committed Sep 28, 2017
2 parents 983a52a + 432e8b9 commit 664078f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
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

0 comments on commit 664078f

Please sign in to comment.