Thursday, July 7, 2011

Mocking LlblGen RetrievalProcedures

If you need to create a mock for one of the LlblGen classes such as RetrievalProcedures, you are faced with the problem that there is no interface for you to use.  Fortunately most of the classes generated by LlblGen are partials so you can use add your own code to them.

Suppose you need to mock out RetrievalProcedures and that it has a method called GetPins you want to mock:

public partial class RetrievalProcedures
{
    public virtual DataTable GetPins(System.Int32 numberOfPins)
    {
        using(DataAccessAdapter dataAccessProvider = new DataAccessAdapter())
        {
            return GetPins(numberOfPins, dataAccessProvider);
        }
    }

    public virtual DataTable GetPins(System.Int32 numberOfPins, IDataAccessCore dataAccessProvider)
    {
        using(StoredProcedureCall call = CreateGetPinsCall(dataAccessProvider, numberOfPins))
        {
            DataTable toReturn = call.FillDataTable();            
            return toReturn;
        }
    }

    ...
}

You can create a partial class next to it as well as the interface you need and let the partial class implement the interface:

public interface IRetrievalProcedures
{
    DataTable GetPins(System.Int32 numberOfPins);

    DataTable GetPins(System.Int32 numberOfPins, IDataAccessCore dataAccessProvider);
}

partial class RetrievalProcedures : IRetrievalProcedures
{
}

The original class now implements the new interface and you can go forth and mock.

No comments:

Post a Comment