Shorthand for built-in transformations (`cast`)
Again, the code under test is, so far, pretty trivial:
Nothing is done with the input data except Ecto's built in string-to-integer and string-to-date transformations.
Most schemas are more complicated but–importantly–they are built from parts that aren't. Within more complicated schemas lurk fields just as simple as :age
or :date
. Again, tests should be able to take advantage of stereotyped code. To that end, see line 4:
Line 4 declares that, in the case of successful validation
, these two fields will have the values that Changeset.cast
gives them, so there's no need for an explicit changeset
clause in the :ok
example.
as_cast
also works with validation failures. Here's an example that narrows in on what a bad date parameter looks like:
Again, no changeset
clause is required. When the test runner calls Changeset.cast
, it will get a changeset with no changes for date
and an error message associated with that field. It will check that the function under test (Basic.changeset
) produces what Changeset.cast
does.
That might seem pointless: aren't you checking that cast
does what cast
does?
That's roughly true. There aren't very many bugs involving such fields that wouldn't be caught by tests written for other purposes. Quite often, I wouldn't be tempted to write explicit assertions about those fields. On the other hand, if it's as easy as typing the field name once after as_cast:
, why not?
Other field transformations are more useful.
Last updated
Was this helpful?