You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a continuation of #452 (reported by @toha73).
publicinterfaceIInterface{stringAnswer{get;}}publicclassImplementation:IInterface{publicstringAnswer=>"answer from implementation";// note: non-overridable!}[Fact]voidSetup_throws_NotSupportedException_when_given_sealed_method(){// Arrange:ImplementationmockedImplementation=Mock.Of<Implementation>();Mock<IInterface>mock=Mock.Get<IInterface>(mockedImplementation);// Act:varerror=Record.Exception(()=>{mock.Setup(_ =>_.Answer).Returns("mocked answer");});// Assert:Assert.NotNull(error);Assert.IsType<NotSupportedException>(error);}
This test creates a mock Implementation, then tries to set up Answer by looking at the mock through the IInterface interface. This setup should fail because Implementation.Answer is sealed, i.e. non-overridable, and therefore non-interceptable.
However, the test currently fails, i.e. the setup (incorrectly) runs to completion without any error. This would probably suggest to most people that the setup will take effect, yet that is not the case: proxy.Answer will (correctly) still return "answer from implementation" and not "mocked answer".
In more straightforward usages, such as new Mock<Implementation>().Setup(_ => _.Answer), Moq correctly recognizes that Implementation.Answer is non-overridable and throws.
The problem here is that Moq only sees IInterface.Answer and doesn't verify what target method (Implementation.Answer) this interface method might map to for the given mock, and whether that target method might be sealed.
This problem can only surface when mocking classes. It can't happen when mocking an interface directly.
The problem also isn't unique to just Setup. For example, SetupGet is likewise affected.
The text was updated successfully, but these errors were encountered:
Closing this dormant issue, but marking it as "unresolved" so it can be easily found again. Please see #642 for details. If you'd like to pick this up and work on it, please post here briefly and we'll see what we can do!
This is a continuation of #452 (reported by @toha73).
This test creates a mock
Implementation
, then tries to set upAnswer
by looking at the mock through theIInterface
interface. This setup should fail becauseImplementation.Answer
is sealed, i.e. non-overridable, and therefore non-interceptable.However, the test currently fails, i.e. the setup (incorrectly) runs to completion without any error. This would probably suggest to most people that the setup will take effect, yet that is not the case:
proxy.Answer
will (correctly) still return"answer from implementation"
and not"mocked answer"
.In more straightforward usages, such as
new Mock<Implementation>().Setup(_ => _.Answer)
, Moq correctly recognizes thatImplementation.Answer
is non-overridable and throws.The problem here is that Moq only sees
IInterface.Answer
and doesn't verify what target method (Implementation.Answer
) this interface method might map to for the given mock, and whether that target method might be sealed.This problem can only surface when mocking classes. It can't happen when mocking an interface directly.
The problem also isn't unique to just
Setup
. For example,SetupGet
is likewise affected.The text was updated successfully, but these errors were encountered: