0

I'm trying to test my controllers, but each of them have a dependency on a service which has a dependency on a repository. I have the following code...

Mock.Get(controller.Get<ServiceInterface>())
            .Setup(s => s.GetData())
            .Returns(FakeData.Create<Entity>(25));

I keep getting an error related to the ServiceInterface concrete class not having a default instance for its repository (injected). I'm trying to avoid creating a FakeService, but its looking like that's what I'm going to have to do. Can anyone offer some suggestions?

jsteve81
  • 735
  • 2
  • 6
  • 16

2 Answers2

1

My response to this question shows how you can program your services to fulfill an interface, use that interface as a dependency, and then mock the service to help you in unit testing.

Edit

Not having had any experience with the MoqAutoMocker, my advice is somewhat limited. But I would begin by attempting to use Moq directly, and see if that's causing you any trouble.

var serviceMock = new Mock<IService>();
serviceMock.Setup(r => r.GetData())
    .Returns(FakeData.Create<Entity>(25));
var controller = new MyController(serviceMock.Object);

Once you've ensured that this works, you can introduce the auto-mocking aspect of it:

var autoMocker = new MoqAutoMocker<MyController>();
Mock.Get(autoMocker.Get<IService>()).Setup(r => r.GetData())
    .Returns(FakeData.Create<Entity>(25));
MyController controller = autoMocker.ClassUnderTest;
Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Thanks Stripling...my service already implements an interface. See the comment on the second answer for more information. – jsteve81 Dec 21 '10 at 05:15
  • @jsteve: Does your controller depend on the service interface, or the concrete class? It seems unlikely to me that AutoMock would be aware of the concrete class's dependencies if your constructor specifies that it uses the interface. – StriplingWarrior Dec 21 '10 at 18:16
0

What classes are you trying to test and what classes are you trying to stub out? I would guess that you are trying to test your controller, and want to stub out your service. If that is the case, the concrete implementation of your service, and therefore its dependencies, should not participate in your test at all and should be irrelevant. You should only need to stub out the behavior of the service interface that is needed to test your controller.

I'm only guessing at your intention, so if my answer doesn't make sense, you may need to post more code showing what you are trying to test.

Joshua Flanagan
  • 8,527
  • 2
  • 31
  • 40
  • My controller has a injected dependency on my service, and my service has an injected dependency on a repository. I am trying to mock my service so I can inject it, but the problem I am running into is when I try to mock my service, I get an error related to AutoMocker not being able to inject a mock repository into the mocked service. Hope this explains a bit more. – jsteve81 Dec 21 '10 at 05:14
  • That really doesn't make sense. If your controller depends on the service interface, and the controller does not directly depend on the repository interface, then when you mock the service interface, NOTHING should depend on the repository, in which case it should not show up in your test. Are you using the repository interface in your test setup? Does your FakeData class use it? – Joshua Flanagan Dec 25 '10 at 03:34