I have two test classes Tests\Unit\BlogTest
and Tests\Feature\BlogTest
. In Tests\Unit\BlogTest
, the response should be 422, which is obviously not good for redirection.
Below is BlogController -> Create() method code:
if ($validator->fails()) {
if (app()->runningUnitTests()) {
return response('Validation failed', 422);
}
return redirect()->route('blog_edit', ['id' => $id])
->withErrors($validator)
->withInput();
}
Below code is from Tests\Unit\BlogTest
public function test_blog_create_validation()
{
$reqData = ['title' => null, 'author' => null, 'content' => null];
$response = $this->postJson(
action([BlogController::class, 'create']),
$reqData
)->assertStatus(422);
}
Below code is from Tests\Feature\BlogTest
public function test_blog_create_validation()
{
$response = $this->post('/blog', [
'title' => '',
'author' => '',
'content' => ''
]);
$response->assertSessionHasErrors([
'title',
'author',
'content'
]);
}
Now, the problem is, app()->runningUnitTests())
condition satisfy unit test, without this condition, it satisfy feature test. See below images for the reference:
- Feature test passed without condition, but unit test failed
- Unit test passed with condition but feature test failed
Question: How to satisfy both Unit and Feature tests?