You should care about the number of times a dependency was invoked in many cases.
For example, suppose your have a service that inserts some data into some database by invoking a web service.
public interface IDataInserter
{
void Insert(Data[] data);
}
And assume that in some cases you need to insert some 10000 items. But the web service cannot handle such volume of data in a single call. So you decide to create a decorator that will split the data into multiple chunks and send each chunk in a single request.
public class SplittingDecorator : IDataInserter
{
private readonly IDataInserter m_DataInserter;
public SplittingDecorator(IDataInserter data_inserter)
{
m_DataInserter = data_inserter;
}
public void Insert(Data[] data)
{
var chunks =
data
.Select((d, i) => new {d, i})
.GroupBy(x => x.i/50)
.Select(x => x.Select(y => y.d).ToArray())
.ToList();
foreach (var chunk in chunks)
{
m_DataInserter.Insert(chunk);
}
}
}
When you want to test the SplittingDecorator
class, you will create a mock for the data_inserter
constructor argument.
In such test, you need to assert that the mocked IDataInserter
was invoked some X times when you invoke SplittingDecorator.Insert
with a data of size Y.
For example, if the data size (length of the data
array) is 160 items, you want to check that the mock was invoked 4 times.