Add validation test

This commit is contained in:
Marcus Moore 2024-09-18 11:47:59 -07:00
parent 9b293afaac
commit a6bcd3c0c2
No known key found for this signature in database
2 changed files with 40 additions and 0 deletions

View file

@ -14,4 +14,23 @@ class StoreAccessoryTest extends TestCase implements TestsPermissionsRequirement
->postJson(route('api.accessories.store'))
->assertForbidden();
}
public function testValidation()
{
$this->actingAsForApi(User::factory()->createAccessories()->create())
->postJson(route('api.accessories.store'), [
//
])
->assertStatusMessageIs('error')
->assertMessagesContains([
'category_id',
'name',
'qty',
]);
}
public function testCanStoreAccessory()
{
$this->markTestIncomplete();
}
}

View file

@ -100,5 +100,26 @@ trait CustomTestMacros
return $this;
}
);
TestResponse::macro(
'assertMessagesContains',
function (array|string $keys) {
Assert::assertArrayHasKey('messages', $this, 'Response did not contain any messages');
if (is_string($keys)) {
$keys = [$keys];
}
foreach ($keys as $key) {
Assert::assertArrayHasKey(
$key,
$this['messages'],
"Response messages did not contain the key: {$key}"
);
}
return $this;
}
);
}
}