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
Setup generic methods with derived interfaces
The test setups a generic method twice, first with the derived interface and then with the base interface and for either setup it returns differnt object. Before the update it worked like that without issues but after the update only the setup for the base interface was used. After I changed the registration order, to first setup with the base interface and then with the derived interface, it again worked like a charm. I guessed that the setup with the base interface somehow overwrites the setup with the derived one. Is this intended?
Example:
voidMain(){varmock=newMock<IResolver>();
mock.Setup(x => x.Resolve<IFooController>()).Returns(()=>newMock<IFooController>().Object);
mock.Setup(x => x.Resolve<IController>()).Returns(()=>newMock<IController>().Object);varcontroller= mock.Object.Resolve<IController>();
Console.WriteLine(controller.GetType());// in 4.13.1: works// in 4.16.1: InvalidCastException: Unable to cast object of type 'Castle.Proxies.IControllerProxy' to type 'IFooController'.// works in 4.16.1 if I switch the two setup rows with each other.varfooController= mock.Object.Resolve<IFooController>();
Console.WriteLine(fooController.GetType());}publicinterfaceIResolver{
T Resolve<T>();}publicinterfaceIController{}publicinterfaceIFooController:IController{}
Again there is no real issue anymore, quoting myself:
I just want to let you know there was a breaking change, somewhere between 4.13.1 and 4.16.1, which doesn't show up in the documentation.
Thank you for your work!
The text was updated successfully, but these errors were encountered:
Ah yes, I suspected as much. We've had a few similar reports. Whenever this issue shows up, my suggestion is to order setups from least specific to most specific... a rule that always applied and in recent versions also applies to generic type arguments.
The first (more specific) setup would essentially be shadowed by the second (less specific) one.
The same thing now happens with generic type arguments: a IFooController is a IController but not vice versa, so the less specific setup (for IController) should go first.
created from #1217 @stakx
Example:
Again there is no real issue anymore, quoting myself:
The text was updated successfully, but these errors were encountered: