workabyte
workabyte

Reputation: 3775

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.

Upvotes: 3

Views: 577

Answers (2)

IronSean
IronSean

Reputation: 1578

This will set up your Mock to return expectedResult if your method is called with parameters date1 and date2.

var bar = Mock.Of<foo>(f => f.Method(date1, date2) == expectedResult);

I'm still trying to figure out if there is a way to set it so that the Mocked method returns it's own input, but haven't yet with the new .Of method.

Upvotes: 0

The Vermilion Wizard
The Vermilion Wizard

Reputation: 5415

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

Upvotes: 4

Related Questions