4 things I wish I would have known when first working with Elixir. Typically, the first thing a developer working in a new language needs to get right is workflow. We need to create the quickest feedback loop as possible to maximize our productivity.
i
-helperModule.__info__(:functions)
So you started playing with the Elixir language. Congrats! It's pretty awesome. Jose Valim is a boss.
I'm assuming that at this point, you've downloaded Elixir/Erlang and can run a program. If not, follow that tutorial here and phoenix here to get started.
You're still here, eh? Alright, let's fire up our example program to get started.
Let's first set up an Elixir project - specifically a phoenix app so I can make my tips more meaningful:
$ mix phoenix.new example_project
$ cd example_project/
$ mix phoenix.gen.model User users name age:integer # create users DB table & model
$ <open your editor of choice>
OK cool, now let's introduce our helpful tips for Elixir noobs.
It's fantastic and very similar to railscasts if you're familiar. The episodes are easily digestible (~10 minutes each), approachable (everything from data types, modules, OTP, etc.), and generally well done. At $10 a month, it's well worth the money.
First, to make sure the DB connection doesn't timeout while running our tests, we're going to bump the ownership timeout of our app while in the test env.
# config/test.exs
# Configure your database
config :my_app, MyApp.Repo,
# ...
ownership_timeout: 9_000_000
Next, open test/models/user_test.exs
which should have been generated for you with the mix phoenix.gen.model User users name age:integer
command run above. Let's require IEx
near the top of our file, and place an IEx.pry
in the first test. Additionally, you'll notice a @tag :focus
above the test I want to focus on - this could be considered helpful tip #2.5.
Run the test with an iex session (Elixir’s interactive shell) to jump into a breakpoint and inspect the code. Note, the --trace
option is important not to lose your DB connection while in the iex shell - it will timeout otherwise.
$ iex -S mix test --only focus --trace
defmodule ExampleProject.UserTest do
use ExampleProject.ModelCase
alias ExampleProject.User
require IEx
@valid_attrs %{age: 42, name: "some content"}
@invalid_attrs %{}
@tag :focus
test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
IEx.pry
assert changeset.valid?
end
test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end
end
You can now inspect any variables in your test. IEx
works in controllers and models very well, but I haven't had much success in views FWIW. To continue on and finish your test, enter respawn()
.
OK, I'm going to play off of tip #2 with this one. Still in the same IEx session, inspect changeset with i changeset
. You should see something similar to:
Whoa... this is pretty awesome. It describes the object with almost everything you need to know. It provides the data type, the reference modules, and implemented protocols.
Let's take this a step further. Say I wanted to inspect the source of a Ecto.Changeset
data type. The i helper makes that super easy.
Run the i-helper on the Module:
$ i Ecto.Changeset
Hey, this is also super awesome. I can now follow the Source
path in my editor to inspect the module.
Now that I know what data type and modules to work on the changeset
, how can I figure out what functions to call on the object? Well, that brings me to helpful tip #4.
Still in your IEx session, type out the following commands to figure out what Module functions are at your disposal to work with the changeset. Since an Ecto.Changeset
references
Ecto.Changeset.__info__(:functions)
Since ecto changesets also reference the Map module, let's check out those too:
Map.__info__(:functions)
If you have any recommendations for helpful elixir commands, tools, etc. please feel free to drop a comment below. I would love to hear from you and would love another elixir tool in the ol' tool belt.
Happy coding!