I have an method like this:
public virtual Tuple<int,int> GetQuantities(Entry entry, CartHelper cartHelper)
{
    //something to do
    return new Tuple<int, int>(minQuantity, maxQuantity);
}
and to unit-test it, I write this mock:
ProductMock
    .Setup(
        u => u.GetQuantities(It.IsAny<Entry>(), 
        It.IsAny<CartHelper>()))
    .Returns(new Tuple<int,int>(minQuantity, maxQuantity));
But this code failed to compile, with this error:
Argument 1: cannot convert from '
System.Tuple<int,int>' to 'System.Tuple`2<int,int>'
System.Tuple`2 suggests me about the "anonymous type" behind the Tuple class, but I can't find what going on behind the scene, and how to fix this issue.
Sorry, my bad, I just discovered that our main project is set to .NET 3.5, and it uses Tuple from a custom reference (System.ComponentModel.Composition), and test project is using .NET 4.0, and it use .NET's Tuple class. I don't know how this version-inconsistent come to our solution, but I had to switch to another workaround, instead of using Tuple.
Using return statement having return valueA function (which can only return one value) can produce a single tuple that contains multiple elements in each scenario.
To begin with, MagicMock is a subclass of Mock . class MagicMock(MagicMixin, Mock) As a result, MagicMock provides everything that Mock provides and more. Rather than thinking of Mock as being a stripped down version of MagicMock, think of MagicMock as an extended version of Mock.
var tupletoReturn=Tuple.Create<int, int>(51, 57);
ProductMock.Setup(u => u.GetQuantities(It.IsAny<Entry>(), It.IsAny<CartHelper>())).Returns(tupletoReturn);
if works for me
public class MyClass
    {
        public virtual Tuple<int, int> GetQuantities(Entry entry, CartHelper cartHelper)
        {
            return new Tuple<int, int>(0, 0);
        }
    }
    [TestFixture]
    public class Test
    {
        [Test]
        public void TestMethod()
        {
            var tupleToReturn = Tuple.Create<int, int>(10, 20);
            Mock<MyClass> p = new Mock<MyClass>();
            p.Setup(
           u => u.GetQuantities(It.IsAny<Entry>(),
                   It.IsAny<CartHelper>()))
                      .Returns(tupleToReturn);
        }
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With