需要文件数据的单元测试类



我正在扩展我们的单元测试套件,我遇到了一个特定的类,我正试图找出如何模拟它。我有一个方法,它接受byte[]数组作为参数。在一个完美的世界里,这个字节数组将始终是一个包含某种表单的PDF文件。然后它从该PDF中提取所有表单字段并返回它们。

如何潜在地模拟依赖于文件数据的逻辑?我唯一真正的想法是将pdf包含在项目中,并使用IO读取测试文件,或者尝试动态生成pdf表单,然后提取这些字段。

这是PDF提取器的代码:

public class PdfFormExtractor : IDisposable
{
private readonly PdfReader _pdfReader;
private readonly MemoryStream _newPdf;
private readonly PdfStamper _pdfStamper;
public PdfFormExtractor(byte[] pdf)
{
_pdfReader = new PdfReader(pdf);
_newPdf = new MemoryStream();
_pdfStamper = new PdfStamper(_pdfReader, _newPdf);
}
public FormDto ExtractForm()
{
var pdfFormFields = _pdfStamper.AcroFields;
var form = new FormDto()
{
Fields = pdfFormFields.Fields.Select(n => new FormFieldDto
{
Name = n.Key,
Label = n.Key
}).ToList()
};
return form;
}
#region IDisposable Support
// disposable implementation
#endregion
}

使用资源文件。

在Visual Studio中,只需在测试项目中创建一个资源文件,即可包含要在测试中使用的所有文件。打开resx,你会看到通常的字符串列表。但您不限于字符串:您可以在左上角的下拉列表中选择"文件",然后将文件拖放到resx文件中
执行此操作时,请注意粘贴的文件属性:您可以选择将文件解释为二进制文件(在您的用例中,会显示一个字节[](或文本文件(带有编码,会显示字符串(。

然后,在测试中,您可以使用测试文件的内容引用强类型Resource对象和强类型byte[]。

在测试复杂场景时,这种策略有很多应用程序,尤其是与足够智能的序列化程序/反序列化程序(如Json.NET(配对时

您可以将任何复杂的数据结构序列化为Json,然后在测试中将其作为字符串引用(由Resource文件的类直接公开(,使用简单的JsonConvert.DeserializeObject对其进行反序列化,并直接在业务逻辑上运行测试。

您可以使用Microsoft.Fakes为*.dll生成假程序集。使用Fakes,我们可以弯曲任何属性、方法等的结果,。。

我伪造了Sqlconnection类,它通常是为了嘲讽而强化的。

  1. 右键单击您的程序集(在我的例子中是System.Data(
  2. 创建fakes程序集
  3. 它创建CCD_ 2&stubs
  4. 我们需要使用(ShimsContext.Create(((添加作用域。作用域内的所有内容都将按照您的建议进行操作。

    public void ExtractFormTest()
    {
    using (ShimsContext.Create())
    {
    #region FakeIt
    System.Data.SqlClient.Fakes.ShimSqlConnection.AllInstances.Open = (SqlConnection sqlConnection) =>
    {
    Console.WriteLine("Opened a session with Virtual Sql Server");
    };
    System.Data.SqlClient.Fakes.ShimSqlConnection.AllInstances.Close = (SqlConnection sqlConnection) =>
    {
    Console.WriteLine("Closed the session with Virtual Sql Server");
    };
    System.Data.SqlClient.Fakes.ShimSqlCommand.AllInstances.ExecuteNonQuery = (SqlCommand sqlCommand) =>
    {
    if (sqlCommand.CommandText.ToLower().Contains("truncate table"))
    {
    Console.WriteLine("Ran " + sqlCommand.CommandText + " at Virtual Sql Server");
    return 1;
    }
    return 0;
    };
    System.Data.SqlClient.Fakes.ShimSqlBulkCopy.AllInstances.WriteToServerDataTable = (SqlBulkCopy sqlBulkCopy, DataTable datatable) =>
    {
    Console.WriteLine("Written #" + datatable.Rows.Count + " records to Virtual Sql Server");
    };
    System.Data.Common.Fakes.ShimDbDataAdapter.AllInstances.FillDataSet = (DbDataAdapter dbDataAdapter, DataSet dataSet) =>
    {
    var _dataSet = new DataSet();
    var _dataTable = DataTableHelper.LoadFlatfileIntoDataTable(Path.Combine(dailyEmailFlatfilesDirectory, "Flatfile.txt"), flatfileDelimiter, flatfileDataTableFields, regexPatternMdmValidEmail, traceWriter);
    if (dbDataAdapter.SelectCommand.CommandText.Equals(mdmSqlStorProcForSpFlatfileData))
    {
    while (_dataTable.Rows.Count > 1000)
    _dataTable.Rows.RemoveAt(0);
    }
    else if (dbDataAdapter.SelectCommand.CommandText.Equals(mdmSqlStorProcForStFlatfileData))
    {
    while (_dataTable.Rows.Count > 72)
    _dataTable.Rows.RemoveAt(0);
    }
    dataSet.Tables.Add(_dataTable);
    dataSet = _dataSet;
    return 1;
    };
    #endregion
    #region Act
    FormDto formDto = ExtractForm();
    #endregion
    #region Assert
    // Upto the scope of your method and acceptance criteria
    #endregion
    }
    

    }

希望这能有所帮助!

最新更新