0

I have started learning Rhino Mocks and have been able to follow the examples in the online wiki. I thought I would apply those learnings to a home project of mine and came unstuck after hours of fighting against the technology. Here is what I am trying to do (if I don’t provide enough info, please let me know – I don’t want to just do a huge code dump):

I have a class called DataSurface.

It has a member called unitOfWork, which in turn contains three repositories TaskRepository, WorkRepository and ProjectRepository. Each are exposed as Getter properties.

In DataSurface, there is an InsertNewWorkItem method which calls the Find method on the TaskRepository as follows:

public void InsertNewWorkItem(Entities.Work workItem)
{
    var taskForWorkItem = this.unitOfWork.TaskRepositoryInstance.Find(t => t.ID == workItem.TaskId).First();

    Work newWorkItem = new Work
    {
       DateOfWork = workItem.DateOfWork,
       Description = workItem.Description,
       Duration = workItem.Duration,           
       Task1 = taskForWorkItem,
       Week = workItem.WeekNumber,
    };

        this.unitOfWork.WorkRepositoryInstance.Add(newWorkItem);
        this.unitOfWork.WorkRepositoryInstance.SaveChanges();
}

What I was hoping to verify is that the Find method of the TaskRepository was called.

Because the repository is “2 classes deep” i.e. a member inside a member vis a vis the InsertNewWorkItem method, I could not seem to create the repository mock objects at all.

I feel that what I am trying to do is an anti-pattern.

I also realise that perhaps the code is not mockable, as is. I tried every combination of instantiating the repositories inside the unitofwork (e.g. constructor DI), but every time, Rhino Mocks threw errors.

Are there any testing experts out there who can set me straight.

Cheers!

4

1 に答える 1

0

インターフェイスによってすべてのクラスを抽象化する必要があります。そうすれば、何でもモックできます。はい、N レベルを深くモックできます。

ところで、何らかの方法でクラスに注入unitOfWorkするDataStructureか、クラスで明示的にインスタンス化しますか? それをモックできるようにするには、それを注入する必要があります。一般に、これは経験則です-依存関係を注入し、クラスでインスタンス化しないでください。

于 2012-07-13T12:04:58.770 に答える