matthiasnoback / behat-expect-exception
v0.3.0
2020-01-23 17:27 UTC
Requires (Dev)
- php: ^7.2
- behat/behat: ^3.5
This package is auto-updated.
Last update: 2024-08-24 04:01:54 UTC
README
安装
composer require --dev matthiasnoback/behat-expect-exception
用途
这个库允许你在一步定义中运行预期会抛出异常的代码,然后在另一个步骤定义中允许你验证是否正确捕获了正确的异常。就像使用PHPUnit一样,你可以比较捕获的异常类型与预期类型,并检查实际的异常消息是否包含指定的字符串。
用法示例
use Behat\Behat\Context\Context; use BehatExpectException\ExpectException; final class FeatureContext implements Context { // Use this trait in your feature context: use ExpectException; /** * @When I try to make a reservation for :numberOfSeats seats */ public function iTryToMakeAReservation(int $numberOfSeats): void { /* * Catch an exception using $this->shouldFail(). * If the code in the callable doesn't throw an exception, shouldFail() * itself will throw an ExpectedAnException exception. */ $this->shouldFail( function () use ($numberOfSeats) { // This will throw a CouldNotMakeReservation exception: $this->reservationService()->makeReservation($numberOfSeats); } ); } /** * @Then I should see an error message saying: :message */ public function confirmCaughtExceptionMatchesExpectedTypeAndMessage(string $message): void { $this->assertCaughtExceptionMatches( CouldNotMakeReservation::class, $message ); } /** * @When I make a reservation for :numberOfSeats seats */ public function iMakeAReservation(int $numberOfSeats): void { /* * Catch a possible exception using $this->mayFail(). * If the code in the callable doesn't throw an exception, * then it's not a problem. mayFail() doesn't throw an * ExpectedAnException exception itself in that case. * You can still use assertCaughtExceptionMatches(), but * it will throw an ExpectedAnException if no exception was * caught. */ $this->mayFail( function () use ($numberOfSeats) { // This might throw a CouldNotMakeReservation exception: $this->reservationService()->makeReservation($numberOfSeats); } ); } }