How do you make fun of IEnumerable <T>?

My last misfortune caused by ridicule is not grokking, that I need to actually click the results inside the mock object IEnumerable<T>.

Here's a sample (demo only IEnumerable<T>, and not actually good interaction-based testing!):

using System;
using System.Collections.Generic;
using Rhino.Mocks;
using MbUnit.Framework;

[TestFixture]
public class ZooTest
{
    [Test]
    public void ZooCagesAnimals()
    {
        MockRepository mockery = new MockRepository();

        IZoo zoo = new Zoo();

        // This is the part that feels wrong to create.
        IList<IAnimal> mockResults = mockery.DynamicMock<IList<IAnimal>>();
        IAnimal mockLion = mockery.DynamicMock<IAnimal>();
        IAnimal mockRhino = mockery.DynamicMock<IAnimal>();

        using (mockery.Record())
        {
            Expect.Call(zoo.Animals)
                .Return(mockResults)
                .Repeat.Once();
        }

        using (mockery.Playback())
        {
            zoo.CageThe(mockLion);
            zoo.CageThe(mockRhino);

            Assert.AreEqual(mockResults, new List<IAnimal>(zoo.Animals));
        }       
    }
}


public class Zoo : IZoo
{
    private IList<IAnimal> animals = new List<IAnimal>();

    public void CageThe(IAnimal animal)
    {
        animals.Add(animal);
    }

    public IEnumerable<IAnimal> Animals
    {
        get
        {
            foreach(IAnimal animal in animals)
            {
                yield return animal;
            }
        }
    }
}

public interface IAnimal
{
}

public interface IZoo
{
    IEnumerable<IAnimal> Animals { get;}
    void CageThe(IAnimal animal);
}

I do not like how I got it to work for the following reasons:

  • Using the results IEnumerable<IAnimal>in IList<IAnimal>- because I understand that it checks the results on a bunch.
  • Customizing the contents of the results - which I also understand; but my main point is to check what Zoo.Animalsreturns IEnumerable<IAnimal>, and even better what it uses yield returninternally.

Any suggestions for improvement or easier?

: IEnumerable<T> , . , Zoo , Zoo IEnumerable<IAnimal> yield return .

+5
2

, ? CageThe (IAnimal), , IAnimal?

, IAnimals, , , -, , , , , , - ?

: - ( , , ..):

[TestFixture]
public class ZooTest 
{
    [Test]
    public void ZooCagesAnimals()
    {
        MockRepository mockery = new MockRepository();

        IAnimal mockLion = mockery.Stub<IAnimal>();
        IAnimal mockRhino = mockery.Stub<IAnimal>();

        IZoo zoo = new Zoo();

        zoo.CageThe(mockLion);
        zoo.CageThe(mockRhino);

        List<IAnimal> animals = new List<IAnimal>(zoo.Animals);
        Assert.IsTrue(animals.Contains(mockLion));
        Assert.IsTrue(animals.Contains(mockRhino));
    }
}
+4

, , . , IList, , .

, ,

Assert.IsNotNull(zoo.Animals);

, , . , .:)

, , return, - IEnumerable. ,

zoo.Animals.GetEnumerator();

- , . IEnumerator.MoveNext();

, IEnumerable, , IEnumerable Zoo mock IEnumerable IEnumerable .

, .

+3

All Articles