Home > Enterprise >  C# Moq method in abstract class
C# Moq method in abstract class

Time:05-19

Can someone show me how I can mock the result of a method in a base abstract class? See the very basic sample code below to demonstrate the problem and I need to mock the “GetAge()” method’s result. See the commented line at the end with the ending "<----- FIX here" where I think need to add the fix.

Customer Service

public interface ICustomerService
{
    string GetCustomerDetailsSrv(int age);
}

public class CustomerService : ICustomerService
{
    public string GetCustomerDetailsSrv(int age)
    {
        return $"Name: John Doe. Age: {age}";
    }
}

Base Controller

public abstract class MyBaseController : ControllerBase
{
    public virtual int GetAge()
    {
        return 7;
    }
}

Customer Controller

public class CustomerController : MyBaseController
{
    private readonly ICustomerService _customerSrv;

    public CustomerController(ICustomerService customerSrv)
    {
        _customerSrv = customerSrv;
    }

    
    [HttpGet]
    public string GetCustomerDetails()
    {
        var age = GetAge();
        var result = _customerSrv.GetCustomerDetailsSrv(age);
        return result;
    }
}

Customer Controller Test

[TestClass]
public class CustomerControllerTest
{
    private readonly CustomerController _controller;
    private readonly Mock<ICustomerService> _mockSrv;

    public CustomerControllerTest()
    {
        _mockSrv = new Mock<ICustomerService>();
        _controller = new CustomerController(_mockSrv.Object);
    }

    [TestMethod]
    public void TestGet()
    {
        //Arrange
        int mockAge = 11;
        string expectedResult = $"Name: Alice Smith. Age: {mockAge}";
        // _controller.Setup(Controller => Controller.GetAge()).Returns(mockAge); <----- FIX here
        _mockSrv.Setup(repo => repo.GetCustomerDetailsSrv(mockAge)).Returns(expectedResult); 

        //Act
        var actualResult = _controller.GetCustomerDetails();

        //Assert
        Assert.IsTrue(actualResult == expectedResult);
    }
}

CodePudding user response:

I think the following code achieves what you want.

Creating a Mock from a CustomerController allows the setup the virtual method GetAge while still being able to use the GetCustomerDetails method from the CustomerController class.

[TestClass]
public class CustomerControllerTest
{
    private readonly Mock<CustomerController> _mockController;
    private readonly Mock<ICustomerService> _mockSrv;

    public CustomerControllerTest()
    {
        _mockSrv = new Mock<ICustomerService>();
        _mockController = new Mock<CustomerController>(() => new CustomerController(_mockSrv.Object));
    }

    [TestMethod]
    public void TestGet()
    {
        //Arrange
        int mockAge = 11;
        string expectedResult = $"Name: Alice Smith. Age: {mockAge}";
        _mockController.Setup(Controller => Controller.GetAge()).Returns(mockAge);
        _mockSrv.Setup(repo => repo.GetCustomerDetailsSrv(mockAge)).Returns(expectedResult);

        //Act
        var actualResult = _mockController.Object.GetCustomerDetails();

        //Assert
        Assert.IsTrue(actualResult == expectedResult);
    }
}
  • Related