cypress-laravel icon indicating copy to clipboard operation
cypress-laravel copied to clipboard

Calling certain factory states

Open kingwill101 opened this issue 4 years ago • 2 comments

Is there a way to create certain factories with specific states?

kingwill101 avatar Apr 23 '21 16:04 kingwill101

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.

NoelDeMartin avatar Apr 24 '21 06:04 NoelDeMartin

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();
            }
        });

kingwill101 avatar Apr 27 '21 20:04 kingwill101