Testing the Error Case for Create
Explore how to test failure scenarios for Phoenix create endpoints with ExUnit by asserting HTML error messages and verifying no new users or emails are created. Understand using Floki for precise HTML assertions and Mox for controlling side effects to keep your tests reliable and maintainable.
We'll cover the following...
We'll cover the following...
An error test for create
We can complete our coverage of this endpoint and controller action by testing a failure case. Inside the same describe block POST /users, and under our previous test, add this:
Let’s see the details of the test:
- The test leverages a new custom helper function called
flunk_if_email_is_sent/0, on line 8, that’s defined inNotSkull.ConnCase, right next to our other helper function.
Note: Because our test is a failure case, it needs to make sure that no new users were created as a side effect.
-
On line 10, the test is getting the pre-exercise count of users in the database. This ...