O'Reilly logo

Programming Phoenix by Jose Valim, Bruce Tate, Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unit-Testing Plugs

If your code is worth writing, it’s worth testing. Earlier, we bypassed our authentication plug, so we should test it now. The good news is that since our plug is essentially a function, it’s relatively easy to build a set of tests that will confirm that it does what we need.

Create a test/controllers/auth_test.exs and key in the following contents. We’re going to break the test file into parts to keep things simple.

First, test the authenticate_user function that does the lion’s share of the work:

 defmodule​ Rumbl.AuthTest ​do
 use​ Rumbl.ConnCase
  alias Rumbl.Auth
 
  test ​"​​authenticate_user halts when no current_user exists"​,
  %{​conn:​ conn} ​do
 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required