cypress-laravel
cypress-laravel copied to clipboard
Calling certain factory states
Is there a way to create certain factories with specific states?
This is not supported at the moment, but if you really need it you can always define your own command.
In case you are not sure how to go about it, you can see how the built-in create command is implemented:
- This is the Cypress part: https://github.com/NoelDeMartin/cypress-laravel/blob/master/src/commands.ts#L72..L114
- This is the Laravel part: https://github.com/NoelDeMartin/laravel-cypress/blob/master/src/Http/Controllers/CypressController.php#L42..L51
If you make your own command, you can always send a PR adding support for states to the built-in command, I would probably accept it :). If you are going to do that, we can discuss the API before.
Thanks for the response. I eventually chose to write a command to get what i want. Not optimal but it will work for now
Cypress::command('factory', function (string $modelClass, int $count, string $state) {
$factoryBuilder = factory($modelClass, $count ?? 1);
if ($state) {
$factoryBuilder = $factoryBuilder->states($state);
}
try {
return $factoryBuilder->create();
} catch (\Exception $e) {
return $e->getMessage();
}
});