views:

263

answers:

1

I have a factory that creates job objects in the form of IJob

Public Interface IJobFactory
    Function CreateJobs(ByVal cacheTypes As CacheTypes) As IEnumerable(Of IJob)
End Interface

The interface IJob defines three things

Public Interface IJob
    Sub Execute()
    ReadOnly Property Id() As Integer
    ReadOnly Property JobType() As JobType
End Interface

I am trying to test the consumer of the factory, a class called JobManager. The job manager calles IJobFactory and asks for the collection of IJobs. I can stub that out just fine but I can't vary the collection size without a lot of work.

Is there a simple way to stub out the collection so that I get a range back?

How can I create a stub of IJobFactory.CreateJobs in such a way that I get back a collection of IJob stubs, say 5 or so where the Id of each of the IJob stubs is different. The ids could be 1 through 5 and that would work great.

A: 

I would do create a helper function to set expectations on the factory (c# notation, untested):

private List<IJob> SetExpectedJobs(MockRepository mocks, IJobFactory factory, int n)
{
    List<IJob> result = new List<IJob>();
    for(int i=1; i<=n; i++)
    {
        IJob job = mocks.CreateStub<IJob>();
        Expect.Call(job.Id).Return(i).Repeat.Any();
        result.Add(job);
    }
    Expect.Call(factory.CreateJobs(null)).Return(result).IgnoreArguments();
    return result;
}

and call this function when you set expectation at the beginning of the test. Probably you should pass cacheTypes to this method as well.

Grzenio