Testing Update Operation
Explore how to write effective tests for update operations in Ecto queries. Understand how to balance test coverage and maintenance by focusing on key fields, using factories to set up test data, and asserting changes correctly. This lesson guides you through success and error test cases to ensure data integrity during updates.
We'll cover the following...
Testing update is a combination of the testing for the create and read functions.
The Update function
Let’s first look at the function we’re going to test:
Like create/1, we can see that this function is a very lightweight wrapper around calls to the schema’s changeset function (update_changeset/2 in this case) and then to Repo.update/1.
Tests
Testing the update function will be similar, but we’ll need to insert an existing user to be updated by the code.
Let’s write our success test first:
Let’s take a look at this test:
- The test uses the factory to insert a user in line 6.
- In the instruction starting from line 8, the test is creating a parameter map to pass in. It’s using the