如何使用 Pest 测试 Laravel 中的代码是否可以在生产环境中运行?

问题描述 投票:0回答:1

我在 Laravel 项目中有一个中间件,可以在哨兵中触发一些警报

$status = $response->status(); 
if ($status === 500 && config('app.env') === 'production') {            
   \Sentry\captureException(new \Exception('my message', $status));
}

在我的测试中,我有以下内容:

Saloon::fake([
    PostRequest::class => MockResponse::make(["message"=>"error"], 500),
]);

$this->postJson("/my_url", [
    'body' => 'test'
])->assertJson($response);

$exception = new \Exception('my message', 500);

$sentry = Mockery::mock(\Sentry\State\HubInterface::class);
$sentry->shouldReceive('captureException')->with($exception);

在我的测试覆盖率中,我可以看到没有进入条件,因为测试是在测试环境中执行的,我该怎么办?

php laravel testing phpunit pest
1个回答
0
投票

您可以在测试开始时使用

config()
功能设置环境值。

config(['app.env' => 'production'])

https://laravel.com/docs/10.x/configuration#accessing-configuration-values

© www.soinside.com 2019 - 2024. All rights reserved.