Tests to print validation failure messages from changesets

Hi, all!
I was wondering if there is any way to have ExUnit tests to print validation failure messages from changesets when it seeds the database.
I have this validate_inclusion changeset for a field, and I just spent an awful lot of time testing this one model completely oblivious of a typo I made in my seed script because I thought the message would pop up to me in test if the validation would fail.

I’m probably missing something fundamental on how either ExUnit or changesets work.

What do you mean by seeding in this case?

1 Like
assert changeset.valid?
1 Like

Did you use Repo.insert! or Repo.update! when using the changeset, or did you use Repo.insert or Repo.update. The ! versions of the functions will raise if the changeset fails, whereas the non ! versions will return {:ok, result} | {:error, error} tuples. If you want a noisy failure, use !.

2 Likes

I guess the right term would be a fixture? I’m referring to a function I call with setup that seeds the database for each test!

Oh, this is a great tip… I completely missed that the return format of these functions are totally different. Thanks!

Also… it might be a silly question, but writing code using the bang version is best practice or available mostly for troubleshooting?
Cause when I use it I get all sorts of CaseClauseError that I don’t even understand, but everything seems to work fine when I use the version without it. It makes me feel like I shouldn’t be leaving it like this haha :grimacing: