Using Moq to Stub an interface method [duplicate]

会有一股神秘感。 提交于 2019-12-06 20:51:01

问题


Possible Duplicate:
How to mock a method that returns an int with MOQ

Here's my interface:

public interface ICalenderService
    {
        DateTime Adjust(DateTime dateToAdjust, BusinessDayConvention convention, List<HolidayCity> holidayCities);
    }

I've done some research and it seems you can mock this real easily, but I want to stub this out using Moq so that I can pass the stub into my other class constuctors and have the stub return whatever DateTime I want for its Adjust method.

What's the easiest way to do this?

Edit: I know I can create my own stub in my project, but I'd like to write less code, and I think Moq can let me do that, I just don't know what the syntax looks like.


回答1:


Set up the stub like this:

var calendarServiceStub = new Mock<ICalenderService>();

calendarServiceStub
    .Setup(c => c.Adjust(It.IsAny<DateTime>(), It.IsAny<BusinessDayConvention>(), It.IsAny<List<HolidayCity>>()))
    .Returns(theDateTimeResultYouWant);

Pass calendarServiceStub.Object to the other class's constructor.



来源:https://stackoverflow.com/questions/10505704/using-moq-to-stub-an-interface-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!