Wednesday, May 05, 2004

MbUnit features a specialized fixture for testing IEnumerable/IEnumerator implementations. This fixtures does a number of test automatically to ensure that the implementation follows the enumerator specification.

In order to use the fixture, you must

  1. Tag your class with EnumerationFixture,
  2. Tag some methods with DataProvider attribute. Those methods will provide data to be added to the collections,
  3. Tag some methods with CopyToProvider attribute. Those methods will copy the data feeded as argument into a collection. The returned collection will be the tested collection.
  4. That's it!

In this example, we use the Data method to provide the data. The enumeration of ArrayList and int[] is tested.

[EnumerationFixture]
public class EnumerationFixtureAttributeAttributeTest
{
    private Random rnd = new Random();
    private int count = 100;
    [DataProvider(typeof(ArrayList))]
    public ArrayList Data()
    {
        ArrayList list = new ArrayList();
        for(int i=0;i<count;++i)
        list.Add(rnd.Next());
        return list;
    }
[CopyToProvider(typeof(ArrayList))] public ArrayList ArrayListProvider(IList source) { ArrayList list = new ArrayList(source); return list; } [CopyToProvider(typeof(int[]))] public int[] IntArrayProvider(IList source) { int[] list = new int[source.Count]; source.CopyTo(list,0); return list; } }
posted on Wednesday, May 05, 2004 9:45:00 PM UTC  #    Comments [1]
Monday, June 06, 2005 6:05:54 PM UTC
Peli's Blog
Comments are closed.