Controlling Setup Repetition with Story Pipes

We can eliminate this code in TrueStory with story pipes, like this:

  ...
 # this example is simplified for clarity
 
 def​ configure_web_app(c, ...) ​do
  c
  |> Map.put ​:conn​, set_up_test_conn(...)
 end
 
 def​ logged_in(c, user) ​do
  c
  |> Map.put ​:session​, log_in(new_user, c.conn)
 end
 def​ get_root(c) ​do
  c
  |> Map.put ​:response​, get_root(c.conn)
 end
 
  story ​"​​getting /"​, c
  |> configure_web_app
  |> get_root,
  verify ​do
  assert c.conn.response == ​:redirect
 # more assertions about an unauthorized get
 end
 
  story ​"​​getting /, logged in"​, c
  |> configure_web_app
  |> logged_in(create_user)
  |> get_root,
  verify ​do
  assert c.conn.response ...

Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.