我正在使用 Typemock 进行一些单元测试。就我而言,我正在为一个执行平面文件数据处理的程序编写测试。为了对该程序进行单元测试,我编写了一些存根类,它们实现了实际版本使用的相同接口,但不是写入文件系统,而是包含它们写入的内部字符串。
现在我试图让 Typemock 用测试中的存根变体替换类的实际版本,但它给了我以下错误: System.InvalidOperationException : Nullable object must have a value。
这是我试图用我的存根替换的实际版本(包含更多但错误不在这些行上):
public class BatchRepository : IBatchRepository
{
private readonly string _connectionStringName;
public BatchRepository(string connectionStringName) <-- Error triggers on this line
{
_connectionStringName = connectionStringName;
}
}
存根类:
public class BatchRepositoryStub : IBatchRepository
{
private readonly string _connectionStringName;
public BatchRepositoryStub(string connectionStringName)
{
_connectionStringName = connectionStringName;
}
}
测试类和我的测试方法:
[TestClass]
public class InputTest
{
// Variables
private IBatchRepository _batchRepository;
private ICommunicatieproductRepository _communicatieproductRepository;
// Constants
private const string ConnectionStringName = "Test";
private const string InputFileLocation = "Temp";
private const string ArchiefLocation = "Temp";
private const string ErrorLocation = "Temp";
private const string LoggingLocation = "Temp";
private const int BatchGrootte = 1000;
// Use TestInitialize to run code before running each test
[TestInitialize()]
public void Initialize()
{
_batchRepository = new BatchRepositoryStub(ConnectionStringName);
_communicatieproductRepository = new CommunicatieproductRepositoryStub(ConnectionStringName);
}
[TestMethod]
public void CMBatch_FDInput_NewFileErrorOnEmptyRelatienummer()
{
// Arrange
Isolate.Swap.NextInstance<IBatchRepository>().With(_batchRepository);
Isolate.Swap.NextInstance<ICommunicatieproductRepository>().With(_communicatieproductRepository);
var inputFileProcessor = new InputFileProcessor(InputFileLocation, ArchiefLocation, ErrorLocation, LoggingLocation, BatchGrootte, ConnectionStringName);
}
}
实际过程,这会触发错误
public class InputFileProcessor
{
private readonly string _inputFileLocation;
private readonly string _archiefLocation;
private readonly string _errorLocation;
private readonly string _loggingLocation;
private readonly int _batchGrootte;
private readonly IBatchRepository _batchRepository;
private readonly ICommunicatieproductRepository _communicatieproductRepository;
/// <summary>
/// Constructor
/// </summary>
public InputFileProcessor(string inputFileLocation, string archiefLocation, string errorLocation, string loggingLocation, int batchGrootte, string connectionStringName)
{
_inputFileLocation = inputFileLocation;
_archiefLocation = archiefLocation;
_errorLocation = errorLocation;
_loggingLocation = loggingLocation;
_batchGrootte = batchGrootte;
_batchRepository = new BatchRepository(connectionStringName);
_communicatieproductRepository = new CommunicatieproductRepository(connectionStringName);
}
}
当从 InputFileProcessor 的构造函数调用时,错误会在 BatchRepository 的构造函数上触发。一开始我以为参数connectionstringname为null,但事实并非如此。为什么它最终还是在那条线上?使用交换实例方法,我认为它甚至不会到达那里,但最终会出现在存根类中。我认为我的交换实例的实现有问题,但我无法弄清楚。
我知道这样的测试可能并不完全是单元测试的内容,但它是测试程序输出和输入的最简单方法。例如,我需要确保无效文件触发相应的错误。能够轻松更改输入使其更易于管理。