views:

39

answers:

1

Hi guys, one of my repository class (say PersonRepo) has a delegate as its property something like this

private readonly Func<INameRepo> _nameRepo;

and apart from this it is inherited by a class which itself expects one more object (say the session).

Thus when i intialize this in my test I do something like

var funcNameRepo=autoMock.Mock<Func<INameRepo>>();
_personRepo= new PersonRepo(session,funcNameRepo.Object);

but when i run this test i get the following error:

Unable to cast object of type 'System.Func`1[Repositories.Interfaces.INameRepo]' to type Moq.IMocked`1[System.Func`1[Repositories.Interfaces.INameRepo]]'.

what do you think I am doing wrong here. please help me.

+1  A: 

Why mock the Func<INameRepo>? If you want to mock the INameRepo, create a mock for INameRepo and pass it to your PersonRepo via a lambda (which will be the Func<INameRepo>):

var nameRepo = autoMock.Mock<INameRepo>();
_personRepo = new PersonRepo(session, () => nameRepo.Object);
Patrick Steele
hey thanks. this works.. sorry took some time to reply.. i had removed the func from my constructor, but this suddenly hit me today.