Sunday, September 15, 2019

MOQ Framework - For test driven development

Install Moq using the Nuget package manager.

The dll will be added to you references.

Add the using directive:
using Moq;

Now you can mock your services using the Moq framework.

[Test]
public void MyClass_TestThisScenario_ShouldGetThisResult()
{

//declaration of the service in your unit test case
var mockPassengerService = new Mock<IPassengerService>();


//calling the method which I need to mock. Here it's the GetPassengers() method
mockPassengerService.Setup(a => a.GetPassengers(category,
new DateTime(2019, 08, 16, 14, 00, 00),
mappingList)).Returns(GetMultiplePassengers);

//my second moq example, here I'm sending any string value to the method to be mocked
var mockMailService = new Mock<IMailService>();
mockMailService.Setup(a => a.SendMail(It.IsAny<string>(), It.IsAny<string>()));

}

//returns the results for the mocked class
private List<Passenger> GetMultiplePassengers
{
     return new List<Passenger>()
    {
         new Passenger()
         {}
     };
}



No comments:

Post a Comment