Controlling Setup Repetition with Nested Contexts

When you’re working with a bare-bones testing framework like ExUnit, you’ll experience significant repetition when you set up code. Consider this testing script:

  ...
 
 setup context ​do
  set_up_web_app
 end
 
 test ​"​​index, logged in"​ ​do
  user = new_user context
  log_in user
 
  response = get(​"​​/"​)
  assert response.code == 200
  assert response.template == ​"​​index"
 end
 
 test ​"​​profile, logged in"​ ​do
  user = new_user context
  log_in user
 
  response = get(​"​​/​​#{​user.id​}​​"​)
  assert response.code == 200
  assert response.template == ​"​​profile"
 end
 
 test ​"​​profile, logged out"​ ​do
  response = get(​"​​/"​)
  assert response.code == 404
 end ...

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.