简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM