Tuesday, July 15, 2008

Something that's possible in Boo's trunk that wasn't before...

...An implementation of the AutoMockingContainer wrapper in Boo. Compiles cleanly on svn rev 3018:
namespace AMC

import System
import Rhino.Mocks
import Rhino.Testing.AutoMocking

public abstract class AutoMockingTestFixture:

[property(Mocks)]
private _mocks as MockRepository

[property(Container)]
private _container as AutoMockingContainer

public def constructor():
_mocks = MockRepository();
_container = AutoMockingContainer(_mocks);
_container.Initialize();

public def Create[of T]() as T:
return _container.Create[of T]()

public def Mock[of T(class)]() as T:
return _container.Get[of T]()

public def Provide[of TService, TImplementation]() as void:
_container.AddComponent(typeof(TImplementation).FullName, typeof(TService), typeof(TImplementation))

public def Provide[of TService](instance as object) as void:
_container.Kernel.AddComponentInstance(instance.GetType().FullName, typeof(TService), instance)


The main item of note here is the Mock[of T]() method, which has a constraint on it and looks like this:
public def Mock[of T(class)]() as T:
return _container.Get[of T]()


This maps to the corresponding C# code:
public T Mock() where T : class {
return _container.Get()
}

Pretty cut'n'dry, no? Thanks to Avishay Lavie who is spearheading this effort, it seems (and who also notes that the constraints implementation is still incomplete at this time).