mercredi 20 avril 2022

Laravel: A unit test may not use the database

Here is the code that I am trying to write unit test for:

$towpRepo = \App::make(TransportOrderWithoutPriceRepository::class);
$towp = $towpRepo->findByOrderNumber($orderNumber);
if ($towp) {
   $towp->delete();
}

Here is the findByOrderNumber method:

public function findByOrderNumber($orderNumber)
{
     return TransportOrderWithoutPrice::where('order_number', '=', $orderNumber)->first();
}

Now here is the test code:

$transportOrderWithoutPrice = mockery::mock('Phirater\Domain\TransportOrdersWithoutPrice\TransportOrderWithoutPrice');
$transportOrderWithoutPriceRepo = mockery::mock('Phirater\Domain\TransportOrdersWithoutPrice\TransportOrderWithoutPriceRepository');
$transportOrderWithoutPriceRepo->shouldReceive('findByOrderNumber')->once()->andReturn($transportOrderWithoutPrice);

Now whenever I run the test, it returns the following error:

PhiraterTest\Unit\Phirater\Domain\TransportOrders\CreateTransportOrderFromPriceResponseCommandHandlerTest::testCreateTransportOrder Illuminate\Database\QueryException: SQLSTATE[HY000] [1049] Unknown database 'What are you doing? A unit test may not use the database!' (SQL: select * from transport_orders_without_price where order_number = 1 limit 1)

/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1719
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1704
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:481
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:465
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php:77
/home/vagrant/code/phirater-l51/app/Domain/TransportOrdersWithoutPrice/TransportOrderWithoutPriceRepository.php:167
/home/vagrant/code/phirater-l51/app/Domain/Configuration/Events/Listeners/CleanupTrait.php:59
/home/vagrant/code/phirater-l51/app/Domain/Configuration/Events/Listeners/TransportOrderSubscriber.php:84
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:369
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:200
/home/vagrant/code/phirater-l51/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:467
/home/vagrant/code/phirater-l51/app/Domain/TransportOrders/CreateTransportOrderFromPriceResponseCommandHandler.php:117
/home/vagrant/code/phirater-l51/tests/unit/Phirater/TransportOrders/CreateTransportOrderFromPriceResponseCommandHandlerTest.php:90

What am I doing wrong here? I am on laravel 5.5 and phpunit 6.5.14.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire