Injection of MVC dependency

Asked

Viewed 416 times

1

Galera I’m using dependency injection to use the Mock. I wonder if I need to create an interface for my Model and one for my Controller?

Thank you

  • What technology are you using?

  • I’m using MVC and C#

  • And for addiction injection? What is being used?

  • I’m using Interface for dependency injection in my Model and Controller layers

  • I don’t know if I’d be right just to say yes. In this response, I produce context-only interfaces and DbSets, without necessarily extracting the interfaces of Models and Controllers. I avoid this because I think it is long and unnecessary. If you detail better what you want to use regarding technologies and frameworks, I think I can come up with a better answer.

  • I’m using standard Microsoft MVC 4 without splitting layers in the project. This project has to use unit tests and I realized that the way the system was structured was bad. I am using dependency injection in the Model and Controller layers to be able to perform unit tests. I am using Moq in unit tests. My doubt would be whether I would have to create an Interface for Model and one for Controller to be able to uncouple the classes !!! I’d have another way to do this procedure...

Show 1 more comment

1 answer

2


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

The code of your application, that is, what must be tested, will not go through the process of Mocking just because it’s the part that matters to be tested. It doesn’t make sense to do Mocking of a Controller because it is the one that will receive the entries and execute the actions, returning a result that we do not know what it is. The function of an object of Mock is to burn steps as to the return of results of a method. We specify what a Mock returns when we configure your behavior as follows:

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

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

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

  • Gypsy thanks for the help, now it is understood, I would just like to ask one last question: I am doing the tests in Model and there is the method1 that calls the medoto2 inside it and this method2 calls the database. How could I mock method1?

  • These methods belong to the Model and call the database? That’s right?

  • Yes that’s right...

  • This doesn’t make any sense. A Model does not invoke the data layer. It is the representation of a data layer record. It has something much wrong in its code. But, at last, to Mock the metodo1, just use it like I showed you up there: modelo.Setup(m => m.Metodo1).Returns(true);.

  • Sorry Gypsy is the method 2 I need to mock.... This is possible ?

  • It’s possible, but it won’t be much use doing that, because you said that metodo1 flame metodo2, and the result of metodo1 you’re already mocking.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.