2

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.

Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • You're syntax is getting garbled because of the generics. Please format your code proeprly using the 10101 button. – Kirk Woll Aug 03 '10 at 17:32
  • 1
    What does the stack trace look like when the error is thrown? I'm not seeing anywhere in your code that looks like it should be expecting an IMocked<...>. – dahlbyk Aug 04 '10 at 06:08
  • Could be an Automock issue as it's trying to do resolution of the children types. Can you try the same example without using Automock, just with MOQ? – Igor Zevaka Aug 04 '10 at 06:11

1 Answers1

3

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);
PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
  • hey thanks. this works.. sorry took some time to reply.. i had removed the func from my constructor, but this suddenly hit me today. – Baz1nga Sep 15 '10 at 16:17