liquidsword
1 year ago
174
1
Testing

ReflectionException: Class Tests\Unit\InvalidArgumentException does not exist

Posted 1 year ago by liquidsword

Getting my feet wet with Unit testing but running into an odd error with SPL exceptions

Method in model:

...
    public function sell(int $num = 1)
    {
        if($this->stock >= $num) {
            $difference = $this->stock - $num;
            $this->stock = $difference;
        } else {
            throw new InvalidArgumentException('Stock cannot fall below 0.');
        }

    }
...

My unit test:

    function testStockCannotFallBelowZero()
    {
        $this->expectException(\InvalidArgumentException::class);

        $product = factory(Product::class)->create(['stock' => 5]);
        $product->sell(6);
    }

Error received:

phpunit --filter ProductTest
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

.....E.                                                             7 / 7 (100%)

Time: 1.96 seconds, Memory: 18.00MB

There was 1 error:

1) Tests\Unit\ProductTest::testStockCannotFallBelowZero
ReflectionException: Class Tests\Unit\InvalidArgumentException does not exist

C:\Users\cslagle0105\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php:206
C:\Users\cslagle0105\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php:162

ERRORS!
Tests: 7, Assertions: 6, Errors: 1.

Please sign in or create an account to participate in this conversation.