Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Tuple in a mock?

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.

Edit

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.

like image 402
Quan Mai Avatar asked Feb 15 '12 10:02

Quan Mai


People also ask

Does return give a 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.

What is MagicMock?

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.


1 Answers

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);


        }
    }
like image 107
Massimiliano Peluso Avatar answered Oct 22 '22 12:10

Massimiliano Peluso