Search code examples
c#linqmoq

Mock.Of<foo> Setting return for Method Calls With Parameters


Trying to figure out how to set up a Method that has parameters using Linq to Mock

Mock.Of<foo>(f=> f.Method(It.IsAny<DateTime>(), It.IsAny<DateTime>()) ==
        Func<DateTime,DateTime,List<DateTime> = (date1,date2){ /*stuff*/ });

something like that, have tried a few variations and been digging around the web. I'm confidant I have done this before but for the life of me can't find what im missing.


Solution

  • With Moq, assuming your interface is like this:

    interface foo 
    { 
        List<DateTime> Method(DateTime date1, DateTime date2); 
    }
    

    The syntax I think you're looking for to setup the mock is

    var bar = new Mock<foo>();
    bar.Setup(f => f.Method(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
       .Returns<DateTime,DateTime>((date1, date2) => new List<DateTime> { date1, date2 });
    

    Edit

    After searching around, I found this which I think other syntax which I think is what you are looking for:

    var bar = Mock.Of<foo>();
    Mock.Get(bar).Setup(f => f.Method(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
       .Returns<DateTime,DateTime>((date1, date2) => new List<DateTime> { date1, date2 });