We are writing feature/unit tests for an existing code base, and we are fairly new to test driven development.
We were wondering if it is better to test code using Laravel factories or using a seeded test database ?
Some scenarios gets fairly complicated during the test "Arrange" step, probably because it was written without testing in mind, and the other option is quite expensive as seeding the database would consume a lot of work for it to be meaningful,
I am interested to hear your thoughts on this, and also what do you do with common "Arrange" setups like some tests would have a user with posts each has comments and each has votes setup ?
Personally, I like the seed approach where you create a SQLite database for your tests. The benefit of this approach is that you aren't messing with your local dev database when running tests, which is super clean and convenient. The only potential downside it that it isn't a MySQL database... so, maybe just make and migrate 2 databases. One for dev and one for test.
I tend to use factories for automated tests. Bit more reliable than seeds, since they can change. With factories you program what you are getting in the test.
I use seeds for essential data and also to save me from having to fill in lots of forms each time I refresh a database. Though, I have my seeding setup, so only necessary seeds are run on production.
If I'm doing a lot of factory setups in a test, I usually just create a helper function. Means if you need the same setup in other tests, you can just call the helper function rather than having a lot of duplication. Just make it a protected method without the keyword 'test' at the start.
We personally use factories when applicable (test counts for example). We tend to avoid automated generation in cases were we need to test interactions with models. That way we can easily see what the original data was and what should be expected.