[英]Unit testing coupled methods
在下面的示例中,如果无法设置aggregationInfo对象,如何为Deaggregate()方法编写单元测试?
public class Aggregator
{
private AggregationInfo aggregationInfo;
public List Aggregate(List objects)
{
//set aggregationInfo
}
public List Deaggregate(List aggregatedObjects)
{
//use aggregationInfo for the deagregation
}
}
我将通过调用Aggregate
然后调用Deaggregate
,通过调用具有不同List的Aggregation并在这些情况下验证Deaggregate中的预期行为来提供不同的方案
如果要确保仅对方法进行单元测试,则可以执行以下操作:
public class Aggregator
{
private AggregationInfo aggregationInfo;
private readonly IAggregator aggregator;
private readonly IDeaggragator deaggragotor;
public Aggregator(IAggregator aggregator, IDeaggragator deaggragotor)
{
this.aggregator = aggregator;
this.deaggragotor = deaggragotor;
}
public List Aggregate(List objects)
{
this.aggregationInfo = aggregator.Aggregate(objects);
return someListIDontKnowWhereYouGetThisFrom;
}
public List Deaggregate(List aggregatedObjects)
{
return deaggregator.Deaggregate(objects, this.aggregationInfo);
}
}
然后,您对Aggregator的单元测试可以像这样工作:
var systemUnderTest = new Aggregator(new MockAggregator(), new MockDeaggragator());
这将允许您验证Aggregator
将为IAggregator
和IDeaggragotor
提供正确的参数。
最后,您还可以在单独的单元测试中测试RealDeaggragotor
,这可以满足您的问题。
不确定为什么您的汇总器需要知道该信息,这不应该是汇总返回值的属性吗?
public static class Aggregator
{
public static AggregatedList Aggregate(List objects)
{
// aggregate objects to aggregatedlist and set the aggregationInfo
}
public static List Deaggregate(AggregatedList aggregatedList)
{
// use info from the aggregatedList
}
}
public class AggregatedList
{
public AggregationInfo AggregationInfo { get; set; }
public List AggregatedObjects { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.