28

我正在使用一个在函数中使用 out 参数的库,我需要使用该函数测试我的代码。

因此,通过我在项目的其余部分中一直使用的 Moq,尝试让模拟来拯救我。

问题

我知道下面有一堵文字墙,所以问题(提前)是:

  • 根据下面的线索:Moq 是否可以使用需要通常不自行调用的参数的构造函数来模拟项目?
  • 这是我的测试代码的问题吗?与图书馆?使用验证库?
  • 我在使用没有参数的起订量吗?
  • 我什至从哪里开始调试呢?

更新:到目前为止领先

我认为这是在模拟 IXLRow 接口的模拟方面的一个问题。通常看起来 XLRow 只是从工作簿中实例化而从未通过new XLRow()- 这是一个因素吗?

以下测试通过时(注意:模拟):

   [Fact]
    public void TryGetValueCanReturnTrueForVieldWithAnInteger_WhenAccessingFromRow()
    {
        var workbook = new XLWorkbook();
        workbook.Worksheets.Add("TestWS");
        var wb = workbook.Worksheet("TestWS");
        wb.Cell("A1").Value = "12345";

        // NOTE: Here we're referring to the row as part of an instantiated  
        //       workbook instead of Mocking it by itself
        int output;
        Assert.True(wb.Row(1).Cell("A").TryGetValue(out output));
    }

编码

获取有效 object() 模拟的方法片段:

// ...other code that sets up other parts of the row correctly
int isAnyInt = 0; //I don't care about this value, only the true/false

// set this to false to true to mimic a row being a legitimate integer
mock.Setup(m => m.Cell("B").TryGetValue(out isAnyInt)).Returns(true);

测试快乐路径的 xUnit 测试——获取有效行的模拟,然后确保它通过验证。注意:此测试通过。

    [Fact]
    public void Validate_GivenValidRow_ReturnsValid()
    {
        var mockRow = TestHelper.GetMockValidInvoiceDetailsWorksheetRow();

        var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);
        Assert.True(validationResult.IsValid);
    }

xUnit 测试(基本上,“验证器是否因单元格不是整数而失败?”) 注意:此测试通过。

    [Fact]
    public void Validate_GivenNonNumericClaimantID_ReturnsInvalid()
    {
        int outint = 0;

        // Get a mock of a valid row
        var mockRow = TestHelper.GetMockValidInvoiceDetailsWorksheetRow();

        // change the TryGetValue result to false
        mockRow.Setup(m => m.Cell("B").TryGetValue(out outint)).Returns(false);

        var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);
        Assert.False(validationResult.IsValid);
        Assert.Equal("ClaimantID column value is not a number.", validationResult.Errors.First().ErrorMessage);
    }

验证器(使用 FluentValidation):

public class InvoiceDetailsWorksheetRowValidator : AbstractValidator<IXLRow>
{
    public InvoiceDetailsWorksheetRowValidator()
    {
        RuleFor(x => x.Cell("B"))
            .Must(BeAnInt).WithMessage("ClaimantID column value is not a number.")
            .OverridePropertyName("ClaimantIDColumn");

    }

    private bool BeAnInt(IXLCell cellToCheck)
    {
        int result;
        var successful = cellToCheck.TryGetValue(out result);
        return successful;
    }
}

作为参考,图书馆的方法:

    public Boolean TryGetValue<T>(out T value)
    {
        var currValue = Value;

        if (currValue == null)
        {
            value = default(T);
            return true;
        }

        bool b;
        if (TryGetTimeSpanValue(out value, currValue, out b)) return b;

        if (TryGetRichStringValue(out value)) return true;

        if (TryGetStringValue(out value, currValue)) return true;

        var strValue = currValue.ToString();
        if (typeof(T) == typeof(bool)) return TryGetBasicValue<T, bool>(out value, strValue, bool.TryParse);
        if (typeof(T) == typeof(sbyte)) return TryGetBasicValue<T, sbyte>(out value, strValue, sbyte.TryParse);
        if (typeof(T) == typeof(byte)) return TryGetBasicValue<T, byte>(out value, strValue, byte.TryParse);
        if (typeof(T) == typeof(short)) return TryGetBasicValue<T, short>(out value, strValue, short.TryParse);
        if (typeof(T) == typeof(ushort)) return TryGetBasicValue<T, ushort>(out value, strValue, ushort.TryParse);
        if (typeof(T) == typeof(int)) return TryGetBasicValue<T, int>(out value, strValue, int.TryParse);
        if (typeof(T) == typeof(uint)) return TryGetBasicValue<T, uint>(out value, strValue, uint.TryParse);
        if (typeof(T) == typeof(long)) return TryGetBasicValue<T, long>(out value, strValue, long.TryParse);
        if (typeof(T) == typeof(ulong)) return TryGetBasicValue<T, ulong>(out value, strValue, ulong.TryParse);
        if (typeof(T) == typeof(float)) return TryGetBasicValue<T, float>(out value, strValue, float.TryParse);
        if (typeof(T) == typeof(double)) return TryGetBasicValue<T, double>(out value, strValue, double.TryParse);
        if (typeof(T) == typeof(decimal)) return TryGetBasicValue<T, decimal>(out value, strValue, decimal.TryParse);

        if (typeof(T) == typeof(XLHyperlink))
        {
            XLHyperlink tmp = GetHyperlink();
            if (tmp != null)
            {
                value = (T)Convert.ChangeType(tmp, typeof(T));
                return true;
            }

            value = default(T);
            return false;
        }

        try
        {
            value = (T)Convert.ChangeType(currValue, typeof(T));
            return true;
        }
        catch
        {
            value = default(T);
            return false;
        }
    }

问题

第一次测试通过。但是当我运行这个测试时,它失败了:

   [Fact]
   public void Validate_GivenNonNumericInvoiceNumber_ReturnsInvalid()
    {
        int outint = 0; // I don't care about this value

        // Get a mock of a valid worksheet row
        var mockRow = TestHelper.GetMockValidInvoiceDetailsWorksheetRow();

        mockRow.Setup(m => m.Cell("E").TryGetValue(out outint)).Returns(false);

        // Validates & asserts
        var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);
        Assert.False(validationResult.IsValid);

        // Placed here to ensure it's the only error message. This is where it fails.
        Assert.Equal("InvoiceNumber column value is not a number.",validationResult.Errors.First().ErrorMessage);
    }

但它并没有因为验证没有实现而失败——它失败是因为另一个项目首先是无效的,即使我从获得一个有效的模拟中返回它——同样的有效模拟,否则通过测试。

确切地说,该消息是:

Assert.Equal() 失败

位置:第一个差异在位置 0

预期:InvoiceNumber 列值不是数字。

实际:ClaimantID 列值不是数字。

我希望:

  • 它的工作方式与其他测试的工作方式相同,或者
  • 因为幸福的道路也失败了。

但是当快乐的路径(例如有效的模拟)通过,但测试失败,因为该方法是无效的(通过相同的验证作为“有效”模拟的一部分)......它让我完全困惑。

以供参考

4

2 回答 2

1

我认为您不需要 在库中测试TryGetValue 。

BeAnInt 放在一个单独的类中,比如XLCellHelpers,使用IXLCell的模拟进行测试

为XLCellHelpers创建一个接口,比如IXLCellHelpers,将其注入您的验证器:InvoiceDetailsWorksheetRowValidator

模拟IXLCellHelpers来测试验证器。

像这样:

using System;
                    
public class InvoiceDetailsWorksheetRowValidator : AbstractValidator<IXLRow>
{
private readonly IXlCellHelpers xlCellHelpers;

    InvoiceDetailsWorksheetRowValidator(IXlCellHelpers xlCellHelpers)
    {
        this.xlCellHelpers = xlCellHelpers;
    }
    
    public InvoiceDetailsWorksheetRowValidator()
    {
        RuleFor(x => x.Cell("B"))
            .Must(this.xlCellHelpers.BeAnInt).WithMessage("ClaimantID column value is not a number.")
            .OverridePropertyName("ClaimantIDColumn");
    }
}

public interface IXlCellHelpers
{
    bool BeAnInt(IXLCell cellToCheck);
}

public class XlCellHelpers : IXlCellHelpers
{   
    publi bool BeAnInt(IXLCell cellToCheck)
    {
        int result;
        var successful = cellToCheck.TryGetValue(out result);
        return successful;
    }
}
于 2020-11-15T13:01:08.873 回答
0

尽管我非常喜欢 FluentValidator,但这是我在使用 Fluent Validator 进行测试时讨厌的问题之一。

被测系统:

  public class InvoiceDetailsWorksheetRowValidator : AbstractValidator<IXLRow>
  {
      public InvoiceDetailsWorksheetRowValidator()
      {
        RuleFor(x => x.Cell("B"))
            .Must(BeAnInt).WithMessage("ClaimantID column value is not a number.")
            .OverridePropertyName("ClaimantIDColumn");

        RuleFor(x => x.Cell("E"))
            .Must(BeAnInt).WithMessage("InvoiceNumber column value is not a number.")
            .OverridePropertyName("ClaimantIDColumn");

      }

      private bool BeAnInt(IXLCell cellToCheck)
      {
        int result;
        var successful = cellToCheck.TryGetValue(out result);
        return successful;
      }
  }

单元测试:

  [Fact]
  public void Validate_GivenNonNumericClaimantID_ReturnsInvalid()
  {
     int outint = 0;

     // Get a mock of a valid row
     //var mockRow = TestHelper.GetMockValidInvoiceDetailsWorksheetRow();
       var mockRow = new Mock<IXLRow>();

     // change the TryGetValue result to false
     mockRow.Setup(m => m.Cell("B").TryGetValue(out outint)).Returns(false);

     var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);

     Assert.True(validationResult.Errors.Any
       (x => x.ErrorMessage == "ClaimantID column value is not a number."));

     //Other option:
     //validationResult.Errors.Remove(validationResult.Errors.First(x => x.ErrorMessage == "InvoiceNumber column value is not a number."));
     //Assert.Equal("ClaimantID column value is not a number.", validationResult.Errors.First().ErrorMessage);
  }

 [Fact]
 public void Validate_GivenNonNumericInvoiceNumber_ReturnsInvalid()
 {
     int outint = 0; // I don't care about this value

     // Get a mock of a valid worksheet row
     var mockRow = new Mock<IXLRow>();
     mockRow.Setup(m => m.Cell("E").TryGetValue(out outint)).Returns(false);

     // Validates & asserts
     var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);

     Assert.True(validationResult.Errors.Any
          (x => x.ErrorMessage == "InvoiceNumber column value is not a number."));

     //Other option:
     //validationResult.Errors.Remove(validationResult.Errors.First(x => x.ErrorMessage == "ClaimantID column value is not a number."));
     //Assert.Equal("InvoiceNumber column value is not a number.", validationResult.Errors.First().ErrorMessage);
  }
于 2013-10-03T11:24:30.247 回答