New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache::get()
can't be mocked
#10803
Comments
That's because you don't need to mock the get method on the cache repository, because there isn't one to mock. |
Yes, the docs are totally wrong here, sorry. We're open to PRs though. :) |
Well that's totally untrue. I added a reason straight away.
|
I've fixed this in laravel/framework by instructing mockery to allow mocking protected methods on the Facade base class. Fix should be released on next tag. |
Thanks! 👍 |
Was just hitting this problem - thanks for fixing :) |
Using laravel 5.2 and still having issues with Mockery Cache: |
That's a different error message. |
I'm facing a similar issue with Mockery Cache: |
It's not a bug as such, it's because you're being naive with the mocking. The cache manager is only a proxy, so you can't mock methods that you think exist, because they don't. You'll need to mock the |
To provide a quick solution for the described problem of @priyanshuchhazed (even if it's not issue related and quite some time ago): In a normal Unit test mocking through However if you write an integration test faking an HTTP request you need to condition the mock like this:
|
Work perfectly for me. Thanks |
Here's what I've came up with reading your comment: // arrange
$cacheRepository = Cache::driver();
$cacheRepositorySpy = Mockery::spy($cacheRepository);
Cache::swap($cacheRepositorySpy);
// act
$this->post(/* … */);
// assert
$cacheRepositorySpy->shouldHaveReceived('has');
$cacheRepositorySpy->shouldHaveReceived('get'); – or – $partialCacheMock = Mockery::mock(Cache::driver())->makePartial();
$partialCacheMock->shouldReceive('put')->once();
Cache::swap($partialCacheMock); Maybe this is helpful for others that come across this issue… Actually it seems to be working just fine. I'm wondering if it would be a good idea to make Cache::spy() return the equivalent of my It took me hours to figure this out. Documentation and / or API clearly needs an improvement to be honest. |
Somehow I keep coming back to this and I'm always happy that I left this comment for others and future self. 😄 |
Issue #9729 and #10480 have been closed without fix or explanation.
The code example from the official "Testing" docs not work: http://laravel.com/docs/5.1/testing#mocking-facades
Either the official docs need to be updated, or the
Cache
facade adjusted to make it work.Throws:
The text was updated successfully, but these errors were encountered: