Using Factories for testing Castle ActiveRecord: Part 2 : Rearden

Earlier I posted about using factories for testing Castle ActiveRecord. I now have an update that I call Rearden.  Here is is:

using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
 
namespace Rearden
{
    public delegate void ModifyClass(ActiveRecordClass instance, IDictionary overrides);
 
    ///
    /// This is a factory to create test objects for Castle ActiveRecord.
    ///
    public static class Factory
    {
        // overload to call without overrides
        public static ActiveRecordClass Init(ModifyClass modifyClass)
        {
            return Init(modifyClass, new Dictionary());
        }
 
        // instanciate and set values in ActiveRecordClass
        public static ActiveRecordClass Init(ModifyClass modifyClass, IDictionary overrides)
        {
            ActiveRecordClass instance = (ActiveRecordClass)Activator.CreateInstance(typeof(ActiveRecordClass));
            modifyClass(instance, overrides);
            return instance;
        }
 
        // overload to call without overrides
        public static ActiveRecordClass Make(ModifyClass modifyClass)
        {
            return Make(modifyClass, new Dictionary());
        }
 
        // create, save and flush ActiveRecordClass
        public static ActiveRecordClass Make(ModifyClass modifyClass, IDictionary overrides)
        {
            ActiveRecordClass instance = Init(modifyClass, overrides);
            ActiveRecordBase asBase = instance as ActiveRecordBase;
            asBase.SaveAndFlush();
            return instance;
        }
 
    }
 
}

Add the compiled Rearden Assembly to your Test Assembly and then create a Blueprint class:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Quality.Model
{
    public static class Blueprints
    {
        public static void Blueprint(Employee emp, IDictionary overrides)
        {
            emp.FirstName = overrides.ContainsKey("FirstName") ? (string)overrides["FirstName"] : Faker.NameFaker.FirstName();
            emp.LastName = overrides.ContainsKey("LastName") ? (string)overrides["LastName"] : Faker.NameFaker.LastName();
            emp.Xid = overrides.ContainsKey("Xid") ? (string)overrides["Xid"] : Faker.StringFaker.Randomize("X######");
        }
 
        public static void Blueprint(Foo foo, IDictionary overrides)
        {
            foo.Month = overrides.ContainsKey("Month") ? (DateTime)overrides["Month"] : new DateTime(Faker.NumberFaker.Number(2000, 2009), Faker.NumberFaker.Number(1, 12), 1);
            foo.Employee = overrides.ContainsKey("Employee") ? (Employee)overrides["Employee"] :  (Employee)Rearden.Factory.Make(Blueprint);
        }
    }
}

And now your test looks like:

        [Test]
        public void Can_find_by_Xid()
        {
 
            _values["Xid"] = "X123456"; // Dictionary
            Rearden.Factory.Make(Blueprints.Blueprint, _values);
            Employee fromDB = Employee.FindByXid("X123456");
            Assert.AreEqual("X123456", fromDB.Xid);
        }

Tags: , ,

One Response to “Using Factories for testing Castle ActiveRecord: Part 2 : Rearden”

  1. [...] to make a test data factory for Castle ActiveRecord like the Ruby on Rails plugin Machinist and posted about my results. Today, I read a post titled, Test Data Builders: an alternative to the Object Mother Pattern. This [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">