Home > Mobile >  IEnumerable.GetEnumerator unit Test
IEnumerable.GetEnumerator unit Test

Time:12-01

My part of code:

    public class BinaryTree<T> : IEnumerable<T>, IReadOnlyCollection<T> where T : IComparable
        {
.........

             public IEnumerator<T> GetEnumerator()
             {
                  return PreOrder().GetEnumerator();
             }

             IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

.........

I have test for GetEnumerator() method. This test work correctly:

[Fact]
        public void GetEnumerator()
        {
            BinaryTree<int> tree = new BinaryTree<int>();
            int exc = 5;

            tree.Add(5);
            IEnumerator e = tree.GetEnumerator();
            e.MoveNext();
            Object act = e.Current;

            act.Should().Be(exc);
        }

But how test this part of code??

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

CodePudding user response:

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

Creates an explicit implementation of interface IEnumerable. To use it, you need to cast your object as an IEnumerable.

public void GetEnumerator()
{
    BinaryTree<int> tree = new BinaryTree<int>();
    int exc = 5;

    tree.Add(5);
    IEnumerator e = ((IEnumerable)tree).GetEnumerator();
    e.MoveNext();
    Object act = e.Current;

    act.Should().Be(exc);
}
  • Related