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
We want to test a generic function that has multiple invocations of related types. In the test below exception Unable to cast object of type 'Foo' to type 'Bar' is thrown at line mock.Object.Create<Bar>() because x.Create<Foo> setup was invoked. mock.Object.Create<Foo>() also invokes x.Create<Foo>.
Steps to Reproduce
public class Foo;
public class Bar : Foo { };
public interface ICreator
{
T Create<T> ();
}
[TestClass]
public class Test
{
[TestMethod]
public void WrongSetup ()
{
var mock = new Mock<ICreator>(MockBehavior.Strict);
mock.Setup(x => x.Create<Bar>()).Returns(new Bar());
mock.Setup(x => x.Create<Foo>()).Returns(new Foo());
var bar = mock.Object.Create<Bar>();
}
}
Expected Behavior
Default generics setup behavior seems to work like It.IsSubtype<T>. It should default to strict match. If loose match is needed It.IsSubtype<T> can be used.
Describe the Bug
We want to test a generic function that has multiple invocations of related types. In the test below exception
Unable to cast object of type 'Foo' to type 'Bar'
is thrown at linemock.Object.Create<Bar>()
becausex.Create<Foo>
setup was invoked.mock.Object.Create<Foo>()
also invokesx.Create<Foo>
.Steps to Reproduce
Expected Behavior
Default generics setup behavior seems to work like
It.IsSubtype<T>
. It should default to strict match. If loose match is neededIt.IsSubtype<T>
can be used.Exception with Stack Trace
Version Info
4.20.70
Additional Info
Setups can be reversed to make the test pass as child setup is then matched first, but this is not a usable workaround.
The text was updated successfully, but these errors were encountered: