laravel - dependency injection and the IoC Container

后端 未结 1 1776
梦毁少年i
梦毁少年i 2020-12-30 17:46

I\'m trying to wrap my head around dependency injection and the IoC container and i\'m using my UserController as an example. I\'m defining what the UserController depends o

相关标签:
1条回答
  • 2020-12-30 18:28

    If you're worried about a testability thing then you should really only be injecting instances that aren't being routed through a facade, as the facades themselves are testable already (meaning you can mock these in L4). You shouldn't need to inject the response, hasher, view environment, request, etc. By the looks of it you only need to be injecting the $user.

    For everything else I'd just stick to using the static facade. Check out the swap and shouldReceive methods on the base Facade class. You can easily swap out the underlying instance with your own mocked object or simply start mocking with, for example, View::shouldReceive().

    Hope this helps.

    0 讨论(0)
提交回复
热议问题