Given-When-Then (GWT) is a plugin that brings behavioral style testing to Pest:
GWT is a simple API that allows you to structure your tests focused on the behavior. Given-When-Then separation makes the test easier to understand at a glance.
Behavior-driven testing using GWT can help clarify three distinct steps while testing code, which includes 1) arranging the application state (given), 2) the "act" phase (when), and 3) the outcome phase (then) which asserts the expected outcome of the previous steps:
1use App\Exceptions\BlockedUserException; 2use App\Models\User; 3use function Pest\Gwt\scenario; 4use function Pest\Laravel\assertDatabaseHas; 5 6scenario('activate user') 7 ->given(fn() => User::factory()->create()) 8 ->when(fn(User $user) => $user->activate()) 9 ->then(fn(User $user) => assertDatabaseHas('users', [10 'id' => $user->id,11 'activated' => true,12 ]));13 14scenario('activate blocked user')15 ->given(fn() => User::factory()->blocked()->create())16 ->when(fn(User $user) => $user->activate())17 ->throws(BlockedUserException::class);
The repo includes more examples of GWT-style tests for Pest to help you get started. You can learn more about this package, get full installation instructions, and view the source code on GitHub.
0 comments:
Post a Comment
Thanks