@@ -455,49 +455,46 @@ code we could come up with to fulfil our scenario. Something like this:
455455 #[Then('I should have :arg1 product(s) in the basket')]
456456 public function iShouldHaveProductInTheBasket($count)
457457 {
458- // Normally you would import this class - we are using the fully qualified name
459- // to highlight that Behat does not come with an assertion tool (see note below).
460- \PHPUnit\Framework\Assert::assertCount(
461- intval($count),
462- $this->basket
463- );
458+ if (count($this->basket) !== intval($count)) {
459+ throw new \Exception(
460+ sprintf(
461+ 'The basket should have %d item(s), but it has %d.',
462+ intval($count),
463+ count($this->basket)
464+ )
465+ );
466+ }
464467 }
465468
466469 #[Then('the overall basket price should be £:arg1')]
467470 public function theOverallBasketPriceShouldBePs($price)
468471 {
469- \PHPUnit\Framework\Assert::assertSame(
470- floatval($price),
471- $this->basket->getTotalPrice()
472- );
472+ $expectedPrice = floatval($price);
473+ $actualPrice = $this->basket->getTotalPrice();
474+
475+ if ($expectedPrice !== $actualPrice) {
476+ throw new \Exception(
477+ sprintf(
478+ 'Expected basket total price to be %s, but got %s.',
479+ $expectedPrice,
480+ $actualPrice
481+ )
482+ );
483+ }
473484 }
474485 }
475486
476487 As you can see, in order to test and implement our application, we introduced 2 objects -
477488``Shelf `` and ``Basket ``. The first is responsible for storing products and their prices,
478489the second is responsible for the representation of our customer basket. Through appropriate step
479490definitions we declare products' prices and add products to the basket. We then compare the
480- state of our ``Basket `` object with our expectations using PHPUnit assertions .
491+ state of our ``Basket `` object with our expectations and throw exception if the expectations aren't met .
481492
482493.. note ::
483494
484495 Behat doesn't come with its own assertion tool, but you can use any proper assertion
485- tool out there. A proper assertion tool is a library whose assertions throw
486- exceptions on failure. For example, if you're familiar with PHPUnit you can use
487- its assertions in Behat by installing it via composer:
488-
489- .. code-block :: bash
490-
491- $ php composer.phar require --dev phpunit/phpunit
492-
493- and then by simply using assertions in your steps:
494-
495- .. code-block :: php
496-
497- \PHPUnit\Framework\Assert::assertCount(
498- intval($count),
499- $this->basket
500- );
496+ tool out there.
497+ Learn more in the paragraph :ref: `assertion-tools `
501498
502499Now try to execute your feature tests:
503500
0 commit comments