MVC Dependency Injection

1

I'm using dependency injection to use Mock . I would like to know if I need to create an interface for my Model and another one for my Controller ?

Thank you

    
asked by anonymous 24.08.2015 / 18:20

1 answer

2

I do not see how necessary to create an interface for both. In this example, Mocking is performed on top of a service layer and injected using Ninject . In this other, from the ASP website itself .NET MVC , same thing.

The code of your application, ie what is to be tested, will not undergo the Mocking process precisely because it is the part that is of interest to be tested. It does not make sense to Mocking a Controller because it is he who will receive the inputs and perform the actions, returning a result that we do not know what it is. The function of a Mock object is to burn steps for returning a method's results. We specify what a Mock returns when we configure its behavior as follows:

mock.Setup(obj => obj.VerificarSeStringEhVazia("oi")).Returns(true);
mock.Setup(obj => obj.VerificarSeStringEhVazia("")).Returns(false);

Even though, if you want to do the Mock of Controllers , yes, you need to extract the interface from them.

Same thing for Models . However, the gain for your test to do this is very close to zero.

    
24.08.2015 / 21:13