Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

moq

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.

like image 889
workabyte Avatar asked Jan 31 '26 02:01

workabyte


1 Answers

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 });
like image 192
Erik Avatar answered Feb 01 '26 16:02

Erik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!