From 9c5209e365422ac13709a8e3c507f804c6d43f52 Mon Sep 17 00:00:00 2001 From: stakx Date: Sun, 25 Aug 2019 14:20:53 +0200 Subject: [PATCH] Add test from #343 to regression test suite which demonstrates that Moq no longer cares about the precise return type of methods if they otherwise match. This enables some scenarios involving generic methods that previously weren't supported. (It seems correct that Moq no longer tries to match method's return types exactly. While technically not incorrect, it is unusual behavior for most people coming from VB.NET or C#, where the return type is not considered part of the method signature WRT method resolution.) --- .../Regressions/IssueReportsFixture.cs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/Moq.Tests/Regressions/IssueReportsFixture.cs b/tests/Moq.Tests/Regressions/IssueReportsFixture.cs index 52fc51c66..5bf1739bc 100644 --- a/tests/Moq.Tests/Regressions/IssueReportsFixture.cs +++ b/tests/Moq.Tests/Regressions/IssueReportsFixture.cs @@ -1538,7 +1538,38 @@ protected Foo(SerializationInfo info, StreamingContext context) { } } } -#endregion + #endregion + + #region 343 + + public class Issue343 + { + public class Fruit { } + public class Apple : Fruit { } + public class GreenApple : Apple { } + public class Orange : Fruit { } + + public interface IFruitPicker + { + TFruit Pick() where TFruit : Fruit; + } + + [Fact] + public void Return_type_variance_of_generic_method_setup() + { + var fruitPicker = new Mock(); + fruitPicker.Setup(m => m.Pick()).Returns(new Fruit()); + fruitPicker.Setup(m => m.Pick()).Returns(new GreenApple()); // set up method `Apple Pick()` + fruitPicker.Setup(m => m.Pick()).Returns(new Orange()); + + Assert.IsType(fruitPicker.Object.Pick()); + Assert.IsType(fruitPicker.Object.Pick()); + Assert.IsType(fruitPicker.Object.Pick()); // call method `GreenApple Pick()` -- will the setup be matched despite the type difference? + Assert.IsType(fruitPicker.Object.Pick()); + } + } + + #endregion #region 383