In my company code base we have a bunch of extension method for IEnumerable
object.
Some of this method enumerate multiple time the parameter given in entry.
Of course, we don't want the so I'm going to fix those methods.
But first I would like to write some unit test to detect the multiple enumeration.
I come to an implementation of IEnumerable
that provide enumeration count information :
public class TestableEnumerable<T> : IEnumerable<T>
{
readonly T[] values;
public TestableEnumerable(T[] values)
{
this.values = values;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<T> GetEnumerator()
{
EnumerationCount++;
foreach (var value in values)
{
yield return value;
}
}
public int EnumerationCount { get; private set; }
}
The test look like this:
[TestFixture]
public class EnumerableExtensionsTests
{
TestableEnumerable<int> sut;
[SetUp]
public void SetUp()
{
sut = new TestableEnumerable<int>(new[] { -10, 0, 10 });
}
[Test]
public void Append_Enumerate_Once()
{
var result = sut.Append(1).ToArray();
Assert.That(sut.EnumerationCount, Is.EqualTo(1));
}
}
Does NUnit provide any mechanism to accomplish this more lightly ?