I have a middleware in a Laravel project that triggers some alerts in sentry
$status = $response->status();
if ($status === 500 && config('app.env') === 'production') {
\Sentry\captureException(new \Exception('my message', $status));
}
In my test I have the following:
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);
In my test coverage I can see that is not entering in the condition because tests are executed in the testing environment, what can I do?
You can set the environment value at the beginning of the test with the config()
function.
config(['app.env' => 'production'])
https://laravel.com/docs/10.x/configuration#accessing-configuration-values
Note: make sure this change DOES NOT trigger any actions outside of your test setup.