Andrew Stopford's Weblog

@poobah

News

Articles

Family

Old Blogs

MbUnit Factory

Factories in MbUnit work in much the same way in v2 (where they have been available since the start) and v3, in v2 we would use a Factory as follows.

   1:  using System.Collections;
   2:  using MbUnit.Framework;
   3:   
   4:  public class ArrayListFactory
   5:  {
   6:      [Factory]
   7:      public ArrayList ProviderEmptyArrayList
   8:      {
   9:          get { return new ArrayList(); }
  10:      }
  11:   
  12:      [Factory]
  13:      public ArrayList ProviderArrayList
  14:      {
  15:          get
  16:          {
  17:              ArrayList list = new ArrayList();
  18:              list.Add(0);
  19:              list.Add(1);
  20:              return list;
  21:          }
  22:      }
  23:  }

As seen in the TypeFixture example this marks methods as Factory functions that can be reused across tests, for example by a ProviderFactory marked test etc. They are useful way of decorating methods as data providers rather than test methods. Let's look at a v3 example.

   1:  using System;
   2:  using System.Collections;
   3:  using MbUnit.Framework;
   4:   
   5:  [TestFixture]
   6:  public class FactoryTest
   7:  {
   8:      public ArrayList ProviderArrayList
   9:      {
  10:          get
  11:          {
  12:              var list = new ArrayList { 1, 2 };
  13:              return list;
  14:          }
  15:      }
  16:   
  17:      [Test]
  18:      [Factory("ProviderArrayList")]
  19:      public void Test (int value)
  20:      {
  21:          Assert.GreaterThan(value, 0);
  22:      }
  23:  }

MbUnit v3 Factory attributes work differently here in that we mark a given method as a Factory by marking it's given name rather than decorating the actual method, on this example we mark the ProviderArrayList method as a Factory method but decorate our Test method instead.

Posted: Aug 26 2008, 11:23 PM by andrewstopford | with 5 comment(s)
Filed under: ,

Comments

josh said:

ok.. i'm late to the party and a little slow.  I've mostly used nunit, and am not familiar with this concept.  I take it from your v3 example that ProviderArrayList is passed in to the Test my mbunit?  .that's pretty cool.  I just started using mbunit on a project today and will definitely look to apply this.

# August 26, 2008 10:14 PM

AndrewSeven said:

I'm not sure I grok it.

Is the [Factory] a way to specify the rows for [RowTest]?

# August 27, 2008 9:18 AM

Jeff Brown said:

@AndrewSeven

In MbUnit v3, several attributes such as [RowTest] are no longer required.

Instead, any [Test] can optionally be data-driven in 2 easy steps:

1. Define its parameters.

2. Provide its data.

[Row], [Factory], [Column] and others are all just mechanisms for accomplishing item #2.   You can actually use them all at once on the same test.  The data provided by each one will just be combined into a bigger list.

# August 28, 2008 3:21 AM

Recent Faves Tagged With "monospace" : MyNetFaves said:

Pingback from  Recent Faves Tagged With "monospace" : MyNetFaves

# March 22, 2009 11:50 PM

Community Blogs said:

MbUnit 3 has been a long, long labour. It started around early fall of 2007 and under Jeff had grown

# April 2, 2009 9:16 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)