문제

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.

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top