Laravel:单位测试可能无法使用数据库

发布于 2025-01-22 13:13:59 字数 2997 浏览 0 评论 0原文

这是我要编写单元测试的代码:

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

以下是FindbyOrderNumber方法:

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

现在是测试代码:

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

现在每当我运行测试时,它返回以下错误:

phiratertest \ unit \ phirater \ domain \ transportorders \ createTransPortorder fromPriCeresponSecomMandHandlertest :: TestCreatEtransportorder 照明\ database \ queryException:sqlstate [hy000] [1049]未知数据库'您在做什么?单位测试可能不会使用数据库! (SQL:SELECT *来自Transport_orders_without_price其中order_number = 1限制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

我在这里做错了什么?我在laravel 5.5phpunit 6.5.14上。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文