简体   繁体   English

抽象类中的 C# Moq 方法

[英]C# Moq method in abstract class

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.请参阅下面非常基本的示例代码来演示该问题,我需要模拟“GetAge()”方法的结果。 See the commented line at the end with the ending "<----- FIX here" where I think need to add the fix.请参阅末尾带有“<----- FIX here”的注释行,我认为需要在其中添加修复程序。

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

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.CustomerController创建Mock允许设置虚拟方法GetAge ,同时仍然能够使用CustomerController类中的GetCustomerDetails方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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