Why using Repo to write test Factories?

Every example of factories I’ve seen so far seems to use the Repo directly to insert data into the database.

I was just thinking, why is that the case? If you are using it for tests, wouldn’t you want to ensure that the data you are testing is formatted in the same way it’s used in the application, i.e. going through the context functions?

It seems to me that by using the repo directly you are susceptible to mess things up by inserting data that would never be persisted in the real application flow cause it would be stopped in changeset checks and such.

Am I missing something?

FWIW, this is generally what I do. I tend to find that if this isn’t ergonomic, then your context functions could be improved.


Yeah, that’s what I though. Also, if you update your context you get to break the tests and have to fix it accordingly.

Funnily enough, first time I was in contact with how Elixir guys create factories was in your book, haha :grimacing: