|
MbUnit : TypeFixture
This page last changed on Jul 02, 2005 by peli.
The TypeFixture lets you write a fixture for specific type and apply to a number of instance of this type.This fixture is particularly useful for writing fixtures of interfaces and apply it to all the types that implement the interface. In this tutorial, we are going to write a fixture for the IEnumerable interface (note that this interface is best tested with EnumerationFixture). Fixture logicThe TypeFixture has the following execution logic:
Step 1: Creating the fixtureCreate a new class EnumerableTest and tag it with TypeFixture. The TypeFixture attribute takes the tested type as parameter: Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml using System; using System.Collections; using MbUnit.Framework; [TypeFixture(typeof(IEnumerable))] public EnumerableFixture { } Step 2: Create providersMbUnit needs instance of the tested type to feed them into the different tests. Creating those instances is the job of tester. To do so, you can either
We start by writing two methods that return respectively and enumerator on a empty list and non-empty list: Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml using System; using System.Collections; using MbUnit.Framework; [TypeFixture(typeof(IEnumerable))] public EnumerableFixture { [Provider(typeof(IEnumerable))] public ArrayList ProviderEmptyArrayList() { return new ArrayList(); } [Provider(typeof(IEnumerable))] public ArrayList ProviderArrayList() { ArrayList list = new ArrayList(); list.Add(0); list.Add(1); return list; } } The disadvantage of "hardcoding" methods in the fixture is that we cannot reuse the code for other fixture, for example, for the IList fixture. To avoid this problem you can wrap your "generation" methods in a class factory and use the ProviderFactory to tell MbUnit to use this factory; in order for MbUnit to pick up the results, you need to define public properties (parameterless methods ''don't'' work) with the FactoryAttribute attribute: Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml using System; using System.Collections; using MbUnit.Framework; public class ArrayListFactory { [Factory] public ArrayList Empty { get { return new ArrayList();} } [Factory] public ArrayList TwoElems { get { ArrayList list = new ArrayList(); list.Add(0); list.Add(1); return list; } } } [TypeFixture(typeof(IEnumerator))] [ProviderFactory(typeof(ArrayListFactory), typeof(IEnumerable))] public EnumeratorFixture { } That's much better because we can reuse the factory for other fixtures. Of course, you can attach an arbitrary number of factories and provider methods. Step 3: Add some testsAdd the unit tests as usuals on the IEnumerable instance. These methods must take the tested type the tested type as arguments. For the following code, MbUnit will execute four tests: Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml using System; using System.Collections; using MbUnit.Core.Framework; using MbUnit.Framework; public class ArrayListFactory { [Factory] public ArrayList Empty { get { return new ArrayList(); } } [Factory] public ArrayList TwoElems { get { ArrayList list = new ArrayList(); list.Add(0); list.Add(1); return list; } } } [TypeFixture(typeof(IEnumerable))] [ProviderFactory(typeof(ArrayListFactory), typeof(IEnumerable))] public class EnumerableFixture { [Test] [ExpectedException( typeof(InvalidOperationException), "Current called while cursor is before the first element" )] public void CurrentCalledBeforeMoveNext(IEnumerable en) { IEnumerator er = en.GetEnumerator(); object p = er.Current; } [Test] [ExpectedException( typeof(InvalidOperationException), "Current called while cursor is past the last element" )] public void CurrentCalledAfterFinishedMoveNext(IEnumerable en) { IEnumerator er = en.GetEnumerator(); while(er.MoveNext()); object p = er.Current; } } |
| Document generated by Confluence on Jun 11, 2007 11:56 |