Laravel:单位测试可能无法使用数据库
这是我要编写单元测试的代码:
$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.5
和phpunit 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论