Wrapping Up

After writing our first tests, we dug into how to define the scope of a unit test. We then explored the tools ExUnit provides us to organize our tests, rounding out our exploration with ways to…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Testing Read

Testing Elixir — by Andrea Leopardi, Jeffrey Matthias (50 / 80)

👈 Testing Create | TOC | Testing Update 👉

This next test is very simple. First, let’s take a look at the code in our Users module. We used “Read” in the section header to keep it in line with CRUD, but you’ll notice that we prefer to call our function get/1. This is entirely user preference.

These tests are pretty easy to understand. Because the function under test is reading from the database, our test code will have to insert data before the exercise step. We’re using our factory to insert the data into the database at line 3. One of the many benefits of using a factory is that it provides an easy and consistent way to insert data into the database without relying on your own application code.

It’s very important to note, though, that because it isn’t using your application code, the inserted data won’t be run through your changeset functions. If rules about the data are present in the changeset logic but aren’t reflected in the database, you need to make sure that your factory function only returns valid data. That’s testing a read function.

👈 Testing Create | TOC | Testing Update 👉

Add a comment

Related posts:

Students Partner with the Homeless in Trenton

A truly fine service-learning project — now in progress — from CWI Summer EAST alum Kimby Heil’s students in Trenton, NJ. Kimy’s students are also currently working on an article for Community Works…

Introduction To Gardening.

Hydroponic gardening is the developing of vegetations without soil, usually inside. If you've been actually gardening for any amount of time, you are actually most likely more knowledgeable than…

Steel Strapping

Stainless steel strapping is used to secure materials in a variety of applications. These include bundling, load stabilization, unitizing, bailing wire and more. It also helps prevent damage to…