Mocking Func property of a class
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.I开发者_StackOverflow中文版NameRepo]' 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);
精彩评论