Skip to content

[8.x] Add expects to built-in Facade testing methods #34932

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

Merged
merged 1 commit into from
Oct 22, 2020

Conversation

jasonmccreary
Copy link
Contributor

The built-in testing layer for Facades wraps many underlying Mockery methods which is very great. But it's missing one of the most important testing methods - expects.

Currently shouldReceive is the only out of the box expectation. However, in most cases, shouldReceive does not provide the desired confidence. This can be confusing when new to testing.

In the end, just because something should receive a call, doesn't mean it did. So shouldReceive must included a count modifier such as once, twice, etc to trigger the desired behavior.

Given that it's often the case where you want to ensure something was called, I believe adding expects can help improve the testing experience within Laravel.

Within Mockery expects is simply an alias for shouldRecieve()->once(), yet it's a far more expressive method (which aligns nicely with Laravel), streamlines the code, and avoids the footgun scenario described above.

Before:

Cache::shouldReceive('remember')
        ->with('testing', 'is', 'important')
        ->once();

After:

Cache::expects('remember')
        ->with('testing', 'is', 'important');

@driesvints driesvints changed the title Add expects to built-in Facade testing methods [8.x] Add expects to built-in Facade testing methods Oct 22, 2020
@taylorotwell taylorotwell merged commit 4fc9579 into laravel:8.x Oct 22, 2020
@jasonmccreary jasonmccreary deleted the facades-expect branch October 22, 2020 16:01
@paras-malhotra
Copy link
Contributor

@jasonmccreary isn't this a breaking change? If the underlying facade accessor class has an expects method, __callStatic would never be called and passed to the accessor class?

@jasonmccreary
Copy link
Contributor Author

@paras-malhotra, possibly. I'd have to try it out. I assume that's the case for all of these testing methods though...

@paras-malhotra
Copy link
Contributor

Yeah it's the same for all testing methods. I'm just saying this should go to master instead of 8x imo
/cc @taylorotwell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants