You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello guys. I have a controller called UserController. And i have a UserService that call getById method that returns some object. How can i mock this call when i test UserController?
I tries this code
` /** @var \Hyperf\Di\Container $c */
$c = \Hyperf\Utils\ApplicationContext::getContainer();
So you're trying to unit test a controller class with dependency injections using Inject DI and mocking some dependency, that's right?
I was looking for this last week and found on php-di documentation that it is within best practices not to unit-test controllers that way.
Not sure if it's the right way of doing this, but i found a work around by reflecting the dependency propriety and setting its value to a mocked version of it.
Activity
Spartaques commentedon Jan 4, 2022
So, i have not found a way to bound some implementation to serviceContainer during testing. It's strange for me.
Castrozan commentedon Oct 21, 2023
So you're trying to unit test a controller class with dependency injections using Inject DI and mocking some dependency, that's right?
I was looking for this last week and found on php-di documentation that it is within best practices not to unit-test controllers that way.
Not sure if it's the right way of doing this, but i found a work around by reflecting the dependency propriety and setting its value to a mocked version of it.
It looks like this.