Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling certain factory states #9

Open
kingwill101 opened this issue Apr 23, 2021 · 2 comments
Open

Calling certain factory states #9

kingwill101 opened this issue Apr 23, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@kingwill101
Copy link

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

@NoelDeMartin
Copy link
Owner

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:

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 NoelDeMartin added the enhancement New feature or request label Apr 24, 2021
@kingwill101
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants