Replies: 1 comment 1 reply
-
you should register this just like you would in a service provider. $this->app->bind(CacheRepository::class, $yourMock); otherwise the container doesn't know you want to mock the CacheRepository class. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
9.52.16
PHP Version
8.1.22
Database Driver & Version
No response
Description
Hello 👋
I wan't to mock a simple cache repository class, that is injected in another class constructor (and used in several methods of this class) like so :
In my test, I wan't to mock it :
So the class (
FooService
in this example) in which the cache repository is injected is called, has his method correctly called and stuff, but the mock does not work properly.However, if I edit my class
FooService
with an on-the-flyCacheRepository
class resolution from IOC, like so, my tests works properly :In conclusion it seems that dependencies injected in constructor cannot be mocked properly, but for on-the-fly resolved dependencies it's ok.
FYI,
private readonly
is not the issue here ;)If anybody has an explanation or a workaround to allow both dependencies injected in a constructor AND mock works together.. thanks !
Steps To Reproduce
/
Beta Was this translation helpful? Give feedback.
All reactions