Skip to content
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

[9.x] Add expects to built-in Facade testing methods #34936

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');

@jasonmccreary
Copy link
Contributor Author

jasonmccreary commented Oct 22, 2020

Note: per the discussion in #34932 this is a breaking change.

While likely a low impact, if an underlying Facade object has an expects method, it will no longer be passed through and instead use this concrete expects method. Applications would need to update the name of the underlying method.

@taylorotwell taylorotwell merged commit 7c1fbb3 into laravel:master Oct 22, 2020
@jasonmccreary jasonmccreary deleted the facades-expect branch October 22, 2020 18:52
@GrahamCampbell GrahamCampbell changed the title Add expects to built-in Facade testing methods [9.x] Add expects to built-in Facade testing methods Oct 22, 2020
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.

2 participants