单元测试:模拟服务 returns 默认布尔值 (false) 尽管 Returns() 设置为 true。 C# xunit 最小起订量

Unit Test: Mocked service returns default boolean (false) despite Returns() set to true. C# xunit moq

我被要求使用最小起订量(C# .Net Core 应用程序 3.1)在 xunit 中对以下控制器进行单元测试:

    private readonly IConfigService _configService;
    private readonly IOptions<ApiVersion> _apiVersionSettings;
    private readonly IOptions<MinRequiredDatabaseVersion> _minRequiredDatabaseVersion;

    public InfoController(IConfigService configService, IOptions<ApiVersion> apiVersionSettings, IOptions<MinRequiredDatabaseVersion> minRequiredDatabaseVersion)
    {
        _configService = configService;
        _apiVersionSettings = apiVersionSettings;
        _minRequiredDatabaseVersion = minRequiredDatabaseVersion;
    }

    [HttpGet]
    [ProducesResponseType(typeof(ApplicationVersionDto), 200)]
    [ProducesResponseType(typeof(BadRequestResult), 400)]
    public IActionResult GetVersions()
    {
        // get db version
        var dbVersion = _configService.GetDbVersions();
        // get api version
        var apiVersion = _apiVersionSettings.Value.Version;
        var minReqiredDbVersion = new MinRequiredDataVersion(_minRequiredDatabaseVersion.Value.Major, _minRequiredDatabaseVersion.Value.Minor, _minRequiredDatabaseVersion.Value.BuildNumber);

        var isDatabaseRightVersion = _configService.hasWebSolutionTheRightDatabase(dbVersion, minReqiredDbVersion);
        string currentDbVersion = dbVersion.MajorVersion + "." + dbVersion.MinorVersion + "." + dbVersion.BuildNumber;
        string minDbVersion = minReqiredDbVersion.MajorVersion + "." + minReqiredDbVersion.MinorVersion + "." + minReqiredDbVersion.BuildNumber;
        if (!isDatabaseRightVersion) 
            throw new CustomGisException(CustomGisExceptionName.ArgumentException, 202011101419083007, $"Update the database! current database version {currentDbVersion}, but should have been min. {minDbVersion}", $"Update the database! current database version {currentDbVersion}, but should have been min. {minDbVersion}");

        var result = new ApplicationVersionDto(dbVersion, apiVersion);
        return Json(result);
    }

设置模拟后,模拟的数据库版本服务 (_minRequiredDatabaseVersion) return 的默认布尔值 false 即使模拟设置为 return true,表明某些东西是梨形的:

    [Fact]
    public void GetVersionsGet_ReturnsJsonResult_WhenInvoked()
    {
        // Arrange
        var mockVwCurrentVersionInfo = Mock.Of<VwCurrentVersionInfo>(x => x.BuildNumber == 988 && x.MajorVersion == 988 && x.MinorVersion == 988);
        fixture.mockApiVersion.SetupGet(o => o.Value).Returns(new ApiVersion(){});
        fixture.mockMinRequiredDatabaseVersion.SetupGet(o => o.Value).Returns(new MinRequiredDatabaseVersion(){
            Major=888,
            Minor=888,
            BuildNumber=888});
        fixture.mockConfigService.Setup(x => x.GetDbVersions()).Returns(mockVwCurrentVersionInfo);
        fixture.mockConfigService.Setup(x => x.hasWebSolutionTheRightDatabase(mockVwCurrentVersionInfo, fixture.mockMinRequiredDataVersion.Object)).Returns(true);

        // Act
        var result = fixture.controller.GetVersions();
        var jsonResult = result as JsonResult;

        // Assert
        // Assertions here.
    }

调试时,我在控制器中遇到了 if(!isDatabaseRightVersion) 行,它引发了异常。

如何在这个模拟服务中 return 布尔值 true?

这是我的灯具:

public InfoController controller;
public Mock<IConfigService> mockConfigService;
public Mock<IOptions<ApiVersion>> mockApiVersion;
public Mock<IOptions<MinRequiredDatabaseVersion>> mockMinRequiredDatabaseVersion;
public Mock<VwCurrentVersionInfo> mockVwCurrentVersionInfo;
public Mock<MinRequiredDataVersion> mockMinRequiredDataVersion;
public int vurderingsejendomIdToBeFound;

public InfoFixture()
{
    // Mock services and their setups.
    mockConfigService = new Mock<IConfigService>();
    mockApiVersion = new Mock<IOptions<ApiVersion>>();
    mockMinRequiredDatabaseVersion = new Mock<IOptions<MinRequiredDatabaseVersion>>();
    mockVwCurrentVersionInfo = new Mock<VwCurrentVersionInfo>();
    mockMinRequiredDataVersion = new Mock<MinRequiredDataVersion>();
    mockMinRequiredDataVersion.Object.MajorVersion = 5;
    mockMinRequiredDataVersion.Object.MinorVersion = 5;
    mockMinRequiredDataVersion.Object.BuildNumber = 5;

    // Controller
    controller = new InfoController(mockConfigService.Object, mockApiVersion.Object, mockMinRequiredDatabaseVersion.Object);
}

public void Dispose() {}

提前感谢您的宝贵反馈。

在您的设置中

   fixture.mockConfigService
    .Setup(x => x.hasWebSolutionTheRightDatabase(mockVwCurrentVersionInfo, fixture.mockMinRequiredDataVersion.Object))
    .Returns(true);

fixture.mockMinRequiredDataVersion.Object 不等于 minReqiredDbVersion

作为一个选项,您可以使用 It.IsAny<T> 而不是 fixture.mockMinRequiredDataVersion.Object

      fixture.mockConfigService
    .Setup(x => x.hasWebSolutionTheRightDatabase(mockVwCurrentVersionInfo,It.IsAny< MinRequiredDataVersion >() ))
    .Returns(true);

这样你模拟将适用于 MinRequiredDataVersion

的任何实例

我需要确保 minReqiredDbVersion 具有特定版本,您可以使用回调捕获该参数:

MinRequiredDatabaseVersion capturedMinReqiredDbVersion = null;

fixture.mockConfigService
        .Setup(x => x.hasWebSolutionTheRightDatabase(mockVwCurrentVersionInfo,It.IsAny< MinRequiredDataVersion >() ))
        .Callback( (dbVersion, minReqiredDbVersion) => {capturedMinReqiredDbVersion = minReqiredDbVersion; })  
        .Returns(true);

// Assert
// Assert capturedMinReqiredDbVersion here