nikavr77's avatar

Avoid factories in testing in order for my test suite to run faster.

I am trying to pass an integration test by using a postgres test db.

What I do is to make a POST request to my API and given the below payload: { 'identification_id' => 4, 'filename' => 'example.png', 'type' => 'person' }

In my controller, I have a validation rule that identification_id must exists in the database in the identification table. So, i need to create 4 identification rows with the factory helper in order for test to pass but that slows my test suite in the end. I know that i can pass 'identification_id' => 1 in order to avoid create so much rows but I have business rules where depends on these identification_types and i want to test them. Is there a best practice to avoid create so many rows every time I wanna test it? Maybe is there a solution to run test without validation rules or DB constraints? I don't know..any help? thanks a lot..

0 likes
1 reply
Talinon's avatar

How much does your test suite slow down because of using a factory to create 4 rows? I find things like this are usually pretty negligible.

Are you using an in-memory database? That can speed up your tests drastically.

Another thing you could consider is using the DatabaseTransactions trait for some of your tests. If you are unfamiliar, it prepares your transactions, and then rolls them back at the end of the test, but it doesn't run migrations. So, what you could do, is create a testing database and seed it with your 4 rows. Then every time you run your test suite, your rows will be available.

Please or to participate in this conversation.