Some confusion over testing validations of Contexts

Why is it that in the code generated by phx.gen.html, the create with invalid attributes tests test a whole map invalid attributes? Shouldn’t each attribute be tested one by one

Because if one of the attributes is actually a failing test (meaning a validation didnt happen), the test would still pass? As other attributes are invalid and thus would give an error changeset.

The generators are a baseline and it’s always a tradeoff between being 100% correct and actually getting people started by not creating an overwhelming number of codelines. The values supplied in those attribute maps are meant to be updated with more real live values anyways so I don’t feel it’s to far off expecting people to update their test as well when editing the schema’s changeset function (a.k.a adding their own business rules). Without editing that function the generator does know things are correct anyways.

Edit:

The “Number of Pets” example here does exactly that: Add another test to ensure any custom business rules are save.

Thanks for the prompt reply! However, I still don’t fully get it. So is it recommended that I test every single attribute that can possibly go wrong?

For example, in a ‘User’ schema, should i have 1 test for valid attributes, 1 for invalid username, 1 for invalid password, 1 for invalid email, 1 for non-unique email and 1 for non-unique username? It feels like a little too much.

That’s up to you. If you feel a single test is enough – go with it. If you don’t – add those additional tests. It also depends on the business rules around it. E.g. a decorative color associated to an user, which does easily fallback probably isn’t as important as having a correct email for an user.