Testing Create Operation
Explore how to write tests for the create operation in Ecto queries using ExUnit. Understand how to verify successful data insertion and handle error scenarios effectively. Learn the importance of testing both return values and side effects to ensure data integrity in your Elixir applications.
We’re going to start by looking at part of the logic file with our queries. The file, called Users, contains the basic CRUD actions:
create/1get/1update/2delete/1
The file lib/users/users.ex in testing_ecto is provided:
create/1
The create/1 function in testing_ecto/lib/users/users.ex look like this:
It’s pretty basic, as most CRUD queries are. As a result, testing it won’t be very complicated, either.
Writing tests
Let’s set up a new file at testing_ecto/test/users/users_test.ex.
Success path test
First, we’ll write a success path test.
In it, set up a basic test file structure and then add a describe block for create/1 and our first test:
Let’s go through our test step-by-step:
-
Observe the common setup block at ...