gpt4 book ai didi

unit-testing - 使用 Autofac 根据条件解析多个 Moq 对象

转载 作者:行者123 更新时间:2023-12-04 04:54:44 28 4
gpt4 key购买 nike

您好,我在创建单元测试时将 Moq 与 Autofac 结合使用。我有一个场景,其中我的 SUT 类型的多个实例取决于构造函数参数。我想对这些实例进行最小起订量。我有一个接口(interface)ISpanRecord:

interface ISpanRecord 
{
RecordType RecordType { get; }
string RecordId { get; set; }
string RecordText { get; set; }
ISpanRecord ParentRecord { get; set; }
List<ISpanRecord> Children { get; }
}

我有另一个接口(interface)IRecordTypeFactory,它在RecordType(它是一个枚举)的基础上提供了一个新的ISpanRecord

interface IRecordTypeFactory
{
ISpanRecord GetNewSpanRecord(RecordType recordType);
}

以上接口(interface)由SUT SpanParser类使用

internal class SpanParser : ISpanParser
{
// Private Vars
private ISpanRecord _spanFile;
private readonly IRecordTypeFactory _factory;
private readonly ISpanFileReader _fileReader;

//Constructor
public SpanParser(ISpanFileReader fileReader)
{
_fileReader = fileReader;
_spanFile = Container.Resolve<ISpanRecord>(TypedParameter.From(RecordType.CmeSpanFile),
TypedParameter.From((List<SpanRecordAttribute>)null));
_factory = Container.Resolve<IRecordTypeFactory>(TypedParameter.From(_fileReader.PublisherConfiguration));
}

// Method under test
public SpanRiskDataSetEntity ParseFile()
{
string currRecord = string.Empty;
try
{
var treeLookUp = Container.Resolve<ITreeLookUp>(TypedParameter.From(_spanFile),
TypedParameter.From(_fileReader.PublisherConfiguration));

IList<string> filterLines = _fileReader.SpanFileLines;

ISpanRecord currentRecord;
ISpanRecord previousRecord = _spanFile;
List<string> spanRecords;

foreach (var newRecord in filterLines)
{
currRecord = newRecord;

//check if we got multiple type of records in a single line.

spanRecords = _fileReader.PublisherConfiguration.GetMultipleRecordsText(newRecord);
if (spanRecords == null)
continue;

foreach (var recordText in spanRecords)
{
RecordType recordType = _fileReader.PublisherConfiguration.GetRecordType(recordText);

currentRecord = _factory.GetNewSpanRecord(recordType);

// some more logic

GetPreviousRecord(ref previousRecord, currentRecord);
}
}
// private method
return GetSpanRiskDataSet();
}

catch (OperationCanceledException operationCanceledException)
{
// log
throw;
}
}

在上面的类中,在测试的时候,我想在RecordType的基础上得到ISpanRecord的多个对象。像这样的东西:

mockFactory.Setup(fc=> fc.GetNewSpanRecord(It.IsAny<RecordType>).Returns(// an ISpanRecord object on the basis of Recordtype)

由于上面的设置会在循环中验证,所以我想设置多个案例。请让我知道解决方案是什么,或者指出可以解决的问题。

问候

最佳答案

Returns有多个重载,您正在寻找那些需要 Func<RecordType, ISpanRecord> 的重载作为论据。排序后,您可以构建自定义返回逻辑:

mockFactory
.Setup(fc => fc.GetNewSpanRecord(It.IsAny<RecordType>)
.Returns((RecordType rt) =>
{
if (rt.Property == "value") return new DummySpanRecord();
else if (rt.Property2 == "other value") return new FakeSpanRecord();
else return new DefaultSpanRecord();
});

您使用 service locator instead of abstract factory 有什么原因吗? ?如果您的容器没有嵌入到您的 SUT 中,也许测试会更容易(例如,您不必跟踪容器本身和依赖项注册过程)。

关于unit-testing - 使用 Autofac 根据条件解析多个 Moq 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14321452/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com